PHP foreach数组和对象把我难住了


PHP foreach array and objects have me stumped

我目前正在使用leafly API,并且遇到了一个障碍。

我正在从他们的API中提取数据用于药房审查,使用以下代码

$data = wp_remote_get(http://data.leafly.com/locations/denver-relief/reviews?skip=0&take=10)

如果我var_dump $data,我得到以下内容(截断,所以在这个问题中不是很长)

array(5) {
    ["headers"]=> array(11) {
        ["server"]=> string(17) "openresty/1.7.4.1"
        ["date"]=> string(29) "Wed, 07 Oct 2015 12:37:51 GMT"
        ["content-type"]=> string(31) "application/json; charset=utf-8"
        ["connection"]=> string(5) "close"
        ["vary"]=> array(2) {
            [0]=> string(15) "Accept-Encoding"
            [1]=> string(81) "X-Requested-With, Accept, Host, X-Language-Locale, Prefer-Html-Meta-Tags, Version"
        }
        ["cache-control"]=> string(20) "public, max-age=3600"
        ["expires"]=> string(29) "Wed, 07 Oct 2015 13:39:24 GMT"
        ["last-modified"]=> string(29) "Wed, 07 Oct 2015 12:39:24 GMT"
        ["access-control-allow-origin"]=> string(1) "*"
        ["x-ua-compatible"]=> string(16) "IE=edge,chrome=1"
        ["content-encoding"]=> string(4) "gzip"
    }
    ["body"]=> string(2971) "{
        "reviews": [
            { "id": 435377, "username": "Nateju", "date": "'/Date(1438691404518+0000)'/", "meds": 5, "service": 5, "atmosphere": 5, "overallRating": "5", "shopAgain": true, "wouldRecommend": true, "easeOfRegistration": 0, "packagingQuality": 0, "firstVisit": false, "comments": "I will only shop at livewell because of its quality of good, positive staff, and chill environment. 'n'nBen accommodated me when my I missed my number. As eel as making me feel welcome from beginning to end.'n'nCherdneaux was my budtender during this visit. Not only was she friendly but she found common interest and ASKED/CARED about what I was up to for the remainder of the day. Not many people take the time to ask, let alone care.'n#TheeBestDispensary", "starImage": "https://d3h17ltqi8v019.cloudfront.net/stars/5/240?color=fcc91d", "avatar": "https://d3h17ltqi8v019.cloudfront.net/profile/nateju/avatar/300" },
            { "id": 430268, "username": "fritzcrumb", "date": "'/Date(1437747908306+0000)'/", "meds": 3, "service": 3, "atmosphere": 2, "overallRating": "2.66666675", "shopAgain": false, "wouldRecommend": false, "easeOfRegistration": 0, "packagingQuality": 0, "firstVisit": false, "comments": "Did not like the vibe. Prices higher than average.", "starImage": "https://d3h17ltqi8v019.cloudfront.net/stars/2.66666675/240?color=fcc91d", "avatar": "https://d3h17ltqi8v019.cloudfront.net/profile/fritzcrumb/avatar/300" },
            { "id": 419409, "username": "imhungry", "date": "'/Date(1436456628062)'/", "meds": 5, "service": 5, "atmosphere": 5, "overallRating": "5", "shopAgain": true, "wouldRecommend": true, "easeOfRegistration": 0, "packagingQuality": 0, "firstVisit": true, "comments": "My gf and I stopped by here a little over a week ago. We aren't from Colorado, so they explained to us in detail what we could and couldn't buy. They were very professional and nice. We went in there a little nervous as this was our first time buying from a dispensary. I think his name was Brandon? Anyway, he made us feel right at home. If we ever come back to Denver, this is going to be our go-to dispensary. Very good prices and the staff make it a pleasure to shop there. Their medicines are top notch. Highly recommend!", "starImage": "https://d3h17ltqi8v019.cloudfront.net/stars/5/240?color=fcc91d", "avatar": "https://d3h17ltqi8v019.cloudfront.net/profile/imhungry/avatar/300" }
        ],
        "pagingContext": {
            "PageCount": 36,
            "TotalItemCount": 108,
            "PageIndex": 0,
            "PageNumber": 1,
            "PageSize": 3,
            "HasPreviousPage": false,
            "HasNextPage": true,
            "IsFirstPage": true,
            "IsLastPage": false
        }
    }"
    ["response"]=> array(2) {
        ["code"]=> int(200)
        ["message"]=> string(2) "OK"
    }
    ["cookies"]=> array(0) { }
    ["filename"]=> NULL
}

我要做的是拉入一个foreach语句来循环遍历每个评论,这样我就可以在页面上显示它们,只拉出我想要的信息。

这是我目前得到的代码,但它给了我一个invalid argument supplied foreach()错误。

foreach( $data['body']->review as $review) { echo $review['username'] }

任何帮助都将非常感激。我最近才开始使用API,可以完成一些任务,但这已经让我困惑了几天,我似乎无法理解它。

谢谢!

Try…

$body = json_decode($data['body'],true);
//var_dump($body); exit;
//var_dump($body['reviews']); exit;
foreach ($body['reviews'] as $review) {
    echo $review['username'];
}

如果这不起作用,可能是json字符串有问题。json_decode()将返回null,如果它不喜欢你给它的字符串,所以尝试取消注释var_dump (s),以确保你得到一个数组返回。

正文包含JSON,因此您需要使用:

$json = json_decode($data['body'],true);

那么你可以使用:

foreach ($json['reviews'] as $review) {
    echo $review['username'];
}