我的控制器中有一个方法如下:function surcharge_load_data(){ $data = $this->Surcharge_model->surcharge_load_data(); foreach($data->result_array() as $r) { echo $r->surcharge_id; } }Surcharge_model 方法返回一个结果数组。这是我在模型中的方法:function surcharge_load_data(){ $this->db->order_by('surcharge_id', 'DESC'); $query = $this->db->get('surcharges'); return $query->result_array();}上述方法返回的数据如下所示:[{"surcharge_id":"37","surcharge_curr":"EUR","surcharge_name":"MNM","surcharge_unit_id":"3","surcharge_value":"43","surcharge_category_id":"3","created_timestamp":"2019-06-03 06:18:18","updated_timestamp":"2019-09-01 08:19:18"},{"surcharge_id":"36","surcharge_curr":"EUR","surcharge_name":"TOY","surcharge_unit_id":"3","surcharge_value":"433","surcharge_category_id":"3","created_timestamp":"2019-07-09 09:21:21","updated_timestamp":"2019-09-10 09:21:21"}]几个问题如下:(1) 整个数据封装在[]中。这是否意味着它返回了一个带有数组数组的大对象?(2) 为什么会报错,Call to a member function result_array() on array?我认为 $data 是一个结果数组。请问我怎样才能访问它?
2 回答
繁花不似锦
TA贡献1851条经验 获得超4个赞
试试这个,result_array()这里不用用
function surcharge_load_data(){
$data = $this->Surcharge_model->surcharge_load_data();
foreach($data as $r) {//changes
echo $r['surcharge_id'];
}
}
在这里,你正在使用,result_array()所以你需要使用每个对象,$r['surcharge_id']如果你使用,result()那么你需要使用$r->surcharge_id
UYOU
TA贡献1878条经验 获得超4个赞
从您的控制器中,您无法检索该result_array方法,因为它仅存在于您的模型中。
因此,在您的控制器中,您可以从模型中读取答案,例如:
function surcharge_load_data(){
$data = $this->Surcharge_model->surcharge_load_data();
foreach($data as $r) {
//just here you can retrieve your data
...
}
}
您在控制器中收到$data的模型的结果(在返回之前$query->result_array())在哪里
- 2 回答
- 0 关注
- 170 浏览
添加回答
举报
0/150
提交
取消