Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use object of type stdClass as array(php) [duplicate]

Tags:

json

php

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

Please I keep getting this error: Fatal error: Cannot use object of type stdClass as array in C:\XAMMP\xampp\htdocs\Yemi\geograph\table.php on line 31

This is the php I am trying to run:

php code

like image 358
akinboj Avatar asked Mar 03 '12 13:03

akinboj


1 Answers

You are actually trying to access the object as array.

I can guess you are having problem when you are using json_decode which is returning an object and inside the foreach loop you are trying to access it like an associative array.

Passing the second argument as true to json_decode force it to return associative array.

Make the below change.

Change your this code line

$items = json_decode($contents);

To

$items = json_decode($contents, true);
like image 50
Shakti Singh Avatar answered Sep 21 '22 18:09

Shakti Singh