dongwu5801 2016-01-06 20:17
浏览 41
已采纳

仅使用标记创建数组 - Json解码[重复]

This question already has an answer here:

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

</div>
  • 写回答

1条回答 默认 最新

  • dongxin5429 2016-01-06 20:20
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?