Joel Dean
Joel Dean

Reputation: 2454

access an array within a group of other objects and array

I am using the freebase API. When I get the result from the curl operation I decode the json to PHP. Now I am trying to access the text attributed. its buried deeply within this set of objects and arrays.

stdClass Object ( [id] => /m/09c7w0 [property] => stdClass Object ( [/common/topic/description] => stdClass Object ( [valuetype] => string [values] => Array ( [0] => stdClass Object ( [text] => The United States of America, commonly called the United States and colloquially referred to as... [lang] => en [creator] => /user/wikirecon_bot [project] => wikirecon [dataset] => /m/0kj4zz_ [citation] => stdClass Object

This is what I have tried so far but I am getting some errors. $stuff =$data->property->/common/topic/description->text;

I know the error may be as a result of the "/common/topic/description". Should I convert the json respond to the php object or should I tried to find my value in the json first? Is there a way I can access that attribute "text"

Upvotes: 1

Views: 49

Answers (2)

Sven
Sven

Reputation: 70863

Do use json_decode($jsonstring, true) to only get arrays with textual and numeric keys, and no objects.

That way it is way easier to access everything. You even have less restrictions on how the keys are named.

Upvotes: 2

simplyray
simplyray

Reputation: 1200

A bit hard to read your mixture of classes and arrays but try this:

$data->property->{'/common/topic/description'}->values[0]->text

Upvotes: 0

Related Questions