babbaggeii
babbaggeii

Reputation: 7737

Trying to retrieve property of object

I've got an object:

{
"id": "132268893498013",
"name": "The Flavel",
"location": {
"street": "",
"city": "Paignton",
"state": "",
"country": "United Kingdom",
"zip": "",
"latitude": 50.3520464135,
"longitude": -3.57920113147
},
"link": "https://www.facebook.com/pages/The-Flavel/132268893498013",
"website": "http://www.theflavel.org.uk",
"phone": "01752 924008"
}

I can retrieve this object with:

return $pages['data'][0]

i.e. it's the first object in the data array. And I'm trying to retrieve the id with:

return $pages['data'][0]->id;

But I get the following error:

Trying to get property of non-object

What am I doing wrong?

Upvotes: 0

Views: 48

Answers (2)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

Its a JSON and you need to decode before accessing the data as

$json = '{
"id": "132268893498013",
"name": "The Flavel",
"location": {
"street": "",
"city": "Paignton",
"state": "",
"country": "United Kingdom",
"zip": "",
"latitude": 50.3520464135,
"longitude": -3.57920113147
},
"link": "https://www.facebook.com/pages/The-Flavel/132268893498013",
"website": "http://www.theflavel.org.uk",
"phone": "01752 924008"
}';

$data = json_decode($json,true);

echo $data["id"];

Upvotes: 1

Purefan
Purefan

Reputation: 1506

Like @Daan mentioned in the comment, it isn't an object, looks like it could be an array, try

return $pages['data'][0]['id'];

Upvotes: 0

Related Questions