简体   繁体   中英

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.

But I'm getting the following error: 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. 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:

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

Or to use to use it like an array:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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