gpt4 book ai didi

php - Codeigniter - 如何访问 result_array()

转载 作者:行者123 更新时间:2023-12-04 13:18:02 25 4
gpt4 key购买 nike

我的 Controller 中有一个方法如下:

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 是一个结果数组。请问我怎样才能访问它?

最佳答案

试试这个,这里不需要使用result_array()

function surcharge_load_data(){
$data = $this->Surcharge_model->surcharge_load_data();
foreach($data as $r) {//changes
echo $r['surcharge_id'];
}
}

HERE, you are using result_array() so you need to use each object like $r['surcharge_id'] if you use result() then you need to use $r->surcharge_id

关于php - Codeigniter - 如何访问 result_array(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57899105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com