PHP foreach数组和对象让我难过

时间:2015-10-07 12:55:44

标签: php arrays api foreach

我目前正在使用叶子API,并遇到了障碍。

我从他们的API中提取数据以进行药房审核,并使用以下代码

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

如果我var_dump $数据,我得到以下内容(截断,因此在这个问题中它不长)

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​​,并且可以完成一些任务,但这已经困扰了我几天了,我似乎无法理解它。

谢谢!

2 个答案:

答案 0 :(得分:1)

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

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

然后你可以使用:

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

答案 1 :(得分:1)

...试

$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以确保您获得数组。