简体   繁体   中英

Fatal error: Cannot use object of type stdClass as array in CodeIgniter

I'm getting the error "Fatal error: Cannot use object of type stdClass as array in" on line 37 which happens to be

public function getMatkul1(){
$matkul = $this->rest->get('ambilmk', 'json');
foreach ($matkul as $key => $value) {
    if(($value['semester'] % 2 ) == $semester){
    echo '<option value='.$value['kmk'].'>'.$value['mk'].'</option>';
}
}
}

Anyone know what's wrong with the above code? Or what this error means?

PHP shows this error when you are trying to use an PHP object as an array, ie using [] instead of -> .

Since, I don't know what elements are inside $matkul and how are they located, I can only guess that the problem arises due to using [] in $value. eg try replacing

$value['semester']  with  $value->semester

And with kmk & mk too. It might work.

To debug yourself, have to use :

print_r($matkul) or print_r($value)

Then, in the printed result observe how the elements are in the $matlkul or in $value; whether as an object or as an array. If $value is happened to be an object, then you have to use -> for semester, kmk, mk; not [ ].

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