2

How do I get access to the "thumburl" if I do not know the "pageid"?

{
  "continue": {
    "iistart": "2004-12-19T12:37:26Z",
    "continue": "||"
  },
  "query": {
    "pages": {
      "30260": {
        "pageid": 30260,
        "ns": 6,
        "title": "File:Japanese diet inside.jpg",
        "imagerepository": "local",
        "imageinfo": [
          {
            "thumburl": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Japanese_diet_inside.jpg/130px-Japanese_diet_inside.jpg",
            "thumbwidth": 130,
            "thumbheight": 95,
            "url": "https://upload.wikimedia.org/wikipedia/commons/e/e1/Japanese_diet_inside.jpg",
            "descriptionurl": "https://commons.wikimedia.org/wiki/File:Japanese_diet_inside.jpg",
            "descriptionshorturl": "https://commons.wikimedia.org/w/index.php?curid=30260"
          }
        ]
      }
    }
  }
}

With multiple objects in php I can do this imageinfo[0] but If I put $imageurl = $data->query->pages[0]->imageinfo[0]->thumburl; It is not working because it is an object not an array.

How can I do that?

2
  • I think you got your answer Adrian. A few ways... now the hard part is choosing which one! :) Commented Mar 5, 2018 at 21:36
  • Thank you very much for the answers! I am trying all of the options but unsuccessful yet, I do not why. Once I have it working I will mark with the answer I used. Commented Mar 5, 2018 at 23:31

4 Answers 4

3

You can call get_object_vars to get an associative array of the object properties, then get the first of these.

$props = array_values(get_object_vars($data->query->pages));
$imageurl = $props[0]->imageinfo[0]->thumburl;
Sign up to request clarification or add additional context in comments.

3 Comments

I was thinking of get_class_vars, which just returns an array of the property names. I've simplified this.
Yeah, your two-line approach above is clean n workin!
I really like this answer but I do not why is not working :(
3

You could use reset() to get the first element :

$data = json_decode($json) ;
$elem = reset($data->query->pages) ;
$imageurl = $elem->imageinfo[0]->thumburl ;

6 Comments

Does reset() really work with an object rather than an array?
Don't see anything about it in php.net/manual/en/function.reset.php
I cannot explain it, but it does work when I slap that into my test server with the json example. :) So... yeah. Works too!
@Barmar Seems to work in 5.3 too (sandbox.onlinephpfunctions.com/code/…)
It is an undocumented "feature" of reset() and other functions in its family, and the behaviour has been mostly consistent for several decades.
|
2

Another alternative is to decode as an array and re-index so it always starts at 0:

$result = array_values(json_decode($json, true)['query']['pages'])[0];

Then you can access $result['imageinfo']['thumburl'] or append it to the above.

Comments

1

You can loop through it, so you don't need array key like this:

$pages = (array) $data->query->pages;
foreach($pages as $page) {
  $imageinfo = $page->imageinfo[0]->thumburl;
}

But this will get you only last from list of pages. So in case you know there are more pages, you need to store these thumburl in array. Or in case you are sure you want only first, just exit after first loop.

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.