1

this is my code after json_decode:

stdClass Object (
    [batchcomplete] => 
    [query] => stdClass Object (
            [pages] => stdClass Object (
                    [56667] => stdClass Object (
                            [pageid] => 56667
                            [ns] => 0
                            [title] => Hanoi
                            [contentmodel] => wikitext
                            [pagelanguage] => en
                            [touched] => 2015-10-25T20:13:21Z
                            [lastrevid] => 687471695
                            [length] => 53648
                            [fullurl] => https://en.wikipedia.org/wiki/Hanoi
                            [editurl] => https://en.wikipedia.org/w/index.php?title=Hanoi&action=edit
                            [canonicalurl] => https://en.wikipedia.org/wiki/Hanoi
                        )
                )
        )
)

How I can get values [title], [fullurl] and [pageid] using PHP? I don't now how to go through line [56667] => stdClass Object ( because 56667 is dynamic (it depends on request).

4
  • Pass the second parameter true within json_decode($your_string,true) and you'll get an array instead of objects. And I think you were aware of how to use arrays Commented Oct 26, 2015 at 6:48
  • @Uchiha "And I think you were aware of how to use arrays" Not really, code sample would be handy. Commented Oct 26, 2015 at 6:49
  • Did you tried passing the second parameter within json_decode Commented Oct 26, 2015 at 6:53
  • This is how I understood your advice pastebin.com/PE8DMmDp and it worked, thanks! Commented Oct 26, 2015 at 7:17

1 Answer 1

3

You can use reset() to get the first array value. This will NOT require you to know the key.

Try this:

$output = json_decode($output, true); // convert to array so we can use reset.

$output_details = reset($output['query']['pages']);

$output_details['title']; // title
$output_details['fullurl']; // fullurl
$output_details['pageid']; // pageid
Sign up to request clarification or add additional context in comments.

1 Comment

Nice, it worked too. I chose your answer because you wrote the code sample, thanks!

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.