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

1

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.