1

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 '*' ?

svick
  • 236,525
  • 50
  • 385
  • 514
SIndhu
  • 677
  • 3
  • 15
  • 21
  • Sorted ! I found the answer here : http://stackoverflow.com/questions/13078453/how-do-i-parse-a-php-object-when-the-attribute-name-is?rq=1 – SIndhu Aug 10 '13 at 08:58

2 Answers2

3

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

Community
  • 1
  • 1
Glavić
  • 42,781
  • 13
  • 77
  • 107
2

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.

Sven
  • 69,403
  • 10
  • 107
  • 109