DolphinGirl
DolphinGirl

Reputation: 21

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?

Upvotes: 0

Views: 349

Answers (1)

Biraj Bora
Biraj Bora

Reputation: 949

PHP shows this error when you are trying to use an PHP object as an array, i.e. 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. e.g. 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 [ ].

Upvotes: 1

Related Questions