简体   繁体   English

php-致命错误:无法将类型为stdClass的对象用作数组

[英]php- Fatal Error: Cannot use object of type stdClass as array

    $store_list = array();
    $this->db->select('user_time');
    $this->db->from('users');
    $this->db->where('staff_id',$userid);
    $result = $this->db->get();

    $result= $result->result();
    foreach($result as $rows){
        array_push($store_list, $rows["user_time"]);
    }
    return $store_list;

I have written a function in a model which contains the above-mentioned code which fetches user's time when user's id passed as a parameter and returns user's time it to the controller function. 我在模型中编写了一个函数,其中包含上述代码,当用户ID作为参数传递时,该代码获取用户的时间并将其返回给控制器功能。

But I'm getting the following error: Fatal Error: Cannot use object of type stdClass as array 但是我遇到以下错误: Fatal Error: Cannot use object of type stdClass as array

What's causing this error? 是什么导致此错误? Any ideas? 有任何想法吗?

Note: the sql query returns all matched records. 注意:sql查询返回所有匹配的记录。 It can be one or more than one records. 它可以是一个或多个记录。

可能是$result它不是对象而不是数组,像这样使用

array_push($store_list, $rows->user_time);

The error is because 错误是因为

$result= $result->result();

here $result is an Std Class object, to access its element use -> instead of [] like: 这里$result是一个Std Class对象,使用->而不是[]来访问其元素,例如:

array_push($store_list, $rows->user_time);

Or to use to use it like an array: 或者用来像数组一样使用它:

$result = $this->db->get()->result_array();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM