仅使用标记创建数组 - Json解码

时间:2016-01-06 20:17:39

标签: php json decode

如何使用json decode返回所有urls项“webformatURL”?

{
    "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
}

我试试:

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

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

我已经阅读了手册,但我怀疑我会在实践中看到一个例子 json_decode

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

你的意思是?

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

如果您只想从对象中提取此字段,您可以执行以下操作:

$urls = [];
foreach($test->hits as $hit) {
    $urls[] = $hit->webformatURL;
}
var_dupm($urls);