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
  • 225,720
  • 49
  • 378
  • 501
SIndhu
  • 667
  • 3
  • 14
  • 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ć
  • 41,315
  • 13
  • 72
  • 105
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
  • 66,463
  • 10
  • 102
  • 105