0

How do I return only all the urls item "webformatURL" using json decode?

{
    "totalHits":500,
    "hits":[
        {
            "previewHeight":84,
            "likes":37,
            "favorites":42,
            "tags":"yellow, natural, flower",
            "webformatHeight":360,
            "views":11218,
            "webformatWidth":640,
            "previewWidth":150,
            "comments":8,
            "downloads":6502,
            "pageURL":"https://example.com/en/yellow-natural-flower-715540/",
            "previewURL":"https://example.com/static/uploads/photo/2015/04/10/00/41/yellow-715540_150.jpg",
            "webformatURL":"https://example.com/get/ee34b40a2cf41c2ad65a5854e4484e90e371ffd41db413419cf3c271a6_640.jpg",
            "imageWidth":3020,
            "user_id":916237,
            "user":"916237",
            "type":"photo",
            "id":715540,
            "userImageURL":"https://example.com/static/uploads/user/2015/04/07/14-10-15-590_250x250.jpg",
            "imageHeight":1703
        },

    ],
    "total":7839
}

I try:

    $test= json_decode($json);
    $result= $test->webformatURL;

Error: warning-undefined-property-stdclass::webformatURL

I've read the manual but I doubt then I would see an example in practice json_decode

Thanks for any help

Gislef
  • 1,555
  • 3
  • 14
  • 37

1 Answers1

1

Did you mean?

$result = $test->hits[0]->webformatURL;

If you want to extract only this field from the object, you can do:

$urls = [];
foreach($test->hits as $hit) {
    $urls[] = $hit->webformatURL;
}
var_dupm($urls);
Henrique Barcelos
  • 7,670
  • 1
  • 41
  • 66
  • I was just trying to extract only this field from the object, it works for me. Thank you very much – Gislef Jan 07 '16 at 02:08