0

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?

3
  • 1
    This error means that you treat something that is stdClass object as array. (ta-da!) And it's obviously $value. print_r($value) and see what you have there. Commented Jun 14, 2016 at 16:23
  • it's still not working.. :( but thank you.. :)) Commented Jun 14, 2016 at 16:54
  • Can you just give line no 37 of your php file? Commented Jun 14, 2016 at 18:14

1 Answer 1

1

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 [ ].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.