Reputation: 687
I am trying to use the wikipedia mediawiki api to get a page data. The query url is :
http://en.wikipedia.org/w/api.php?action=parse&format=json&page=Nirnayam_%281991_film%29
One of the properties returned is categories:
"categories":[{"sortkey":"","*":"Telugu-language_films"},{"sortkey":"","*":"1991_films"},{"sortkey":"","*":"Indian_films"}]
A var_dump after json_decode :
foreach($wiki_page_data_json->parse->categories as $cat)
{
var_dump($cat);
}
gives me this :
object(stdClass)[21] public 'sortkey' => string '' (length=0)
public '*' => string 'Telugu-language_films' (length=21)object(stdClass)[22] public 'sortkey' => string '' (length=0)
public '*' => string '1991_films' (length=10)object(stdClass)[23] public 'sortkey' => string '' (length=0)
public '*' => string 'Indian_films' (length=12)
I can access public 'sortkey' as $cat->sortkey
Question is - How do I access the value in public '*' ?
Upvotes: 1
Views: 1401
Reputation: 43582
You can access object properties with names that contain special characters using this notation:
foreach($wiki_page_data_json->parse->categories as $cat)
{
var_dump( $cat->{'*'} );
}
Interesting reading > https://stackoverflow.com/a/10333200/67332
Upvotes: 3
Reputation: 70923
You should probably make json_decode()
only return arrays, and not arrays and objects.
json_decode($jsonstring, true); // last parameter true will return only arrays
Then it is easy:
$cat['sortkey'];
$cat['*'];
I don't really like objects to have inaccessible property names.
Upvotes: 2