简体   繁体   English

致命错误:无法在CodeIgniter中将stdClass类型的对象用作数组

[英]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 我在第37行上收到错误“致命错误:无法将stdClass类型的对象用作数组中的数组”

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 -> . 当您尝试将PHP对象用作数组时,即使用[]而不是->,PHP会显示此错误。

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. 因为我不知道$ matkul中有哪些元素以及它们的位置,所以我只能猜测问题是由于在$ value中使用[]而引起的。 eg try replacing 例如尝试更换

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

And with kmk & mk too. 以及kmk和mk。 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; 然后,在打印结果中观察元素在$ matlkul或$ 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; 如果$ value恰好是一个对象,则必须使用->表示学期,kmk,mk; not [ ]. 不是[]。

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

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