我的控制器中有一个方法如下:
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 是一个结果数组。请问我怎样才能访问它?
繁花不似锦
UYOU