简体   繁体   English

如何使用PHP循环JSON数组

[英]How to loop through JSON array using PHP

My PHP code: 我的PHP代码:

$obj = json_decode($data);
print $obj->{'name'};

While it works for non-arrays, I can't for the life of me figure out how to print all the values within the "Reviews" Array. 虽然它适用于非阵列,但我不能为我的生活弄清楚如何打印“评论”数组中的所有值。

What I would like to do is to loop through this response, probably with forreach(), resulting in a list containing the rating and excerpt for each review in the response. 我想做的是循环这个响应,可能使用forreach(),从而产生一个列表,其中包含响应中每个评论的评级和摘录。

Any guidance / direction is greatly appreciated.. 非常感谢任何指导/方向..

Below is the JSON I'm working with. 下面是我正在使用的JSON。 (it is the response from the Yelp API). (这是来自Yelp API的响应)。

{
    "is_claimed": true,
    "rating": 4.5,
    "mobile_url": "http://m.yelp.com/biz/economy-paint-and-collision-riverside",
    "rating_img_url": "http://s3-media2.ak.yelpcdn.com/assets/2/www/img/99493c12711e/ico/stars/v1/stars_4_half.png",
    "review_count": 19,
    "name": "Economy Paint & Collision",
    "snippet_image_url": "http://s3-media3.ak.yelpcdn.com/photo/ZOzoahw0Go_DEPLvxCaP_Q/ms.jpg",
    "rating_img_url_small": "http://s3-media2.ak.yelpcdn.com/assets/2/www/img/a5221e66bc70/ico/stars/v1/stars_small_4_half.png",
    "url": "http://www.yelp.com/biz/economy-paint-and-collision-riverside",
    "reviews": [
        {
            "rating": 3,
            "excerpt": "The Good:\nDennis quoted me a price over the phone about 1 month before I took my wifes 2010 Escalade in for repairs and when I took it in he gave me the...",
            "time_created": 1357010247,
            "rating_image_url": "http://s3-media3.ak.yelpcdn.com/assets/2/www/img/34bc8086841c/ico/stars/v1/stars_3.png",
            "rating_image_small_url": "http://s3-media3.ak.yelpcdn.com/assets/2/www/img/902abeed0983/ico/stars/v1/stars_small_3.png",
            "user": {
                "image_url": "http://s3-media3.ak.yelpcdn.com/photo/mIsU7ugYd88lLA-XL2q1Cg/ms.jpg",
                "id": "V9MDZvEBv-tBTF4YIoc7mg",
                "name": "Sydney H."
            },
            "rating_image_large_url": "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/e8b5b79d37ed/ico/stars/v1/stars_large_3.png",
            "id": "HfOhzLIlJoUKSKU8euclqA"
        },
        {
            "rating": 5,
            "excerpt": "Dennis and his team did an amazing job on the roof of my fiancee's 2002 Acura RSX after years of living by the beach in San Francisco had mostly rusted...",
            "time_created": 1354741952,
            "rating_image_url": "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/f1def11e4e79/ico/stars/v1/stars_5.png",
            "rating_image_small_url": "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/c7623205d5cd/ico/stars/v1/stars_small_5.png",
            "user": {
                "image_url": "http://s3-media3.ak.yelpcdn.com/photo/ZOzoahw0Go_DEPLvxCaP_Q/ms.jpg",
                "id": "kOqCnCjYn0EbAhtH1tfjcw",
                "name": "Jason H."
            },
            "rating_image_large_url": "http://s3-media3.ak.yelpcdn.com/assets/2/www/img/22affc4e6c38/ico/stars/v1/stars_large_5.png",
            "id": "YzZg1LX6zeRaurq9tYUcMw"
        },
        {
            "rating": 5,
            "excerpt": "It's been a year since I had my car painted here, and I gotta say: It still looks just as good as it did when I first picked it up. You would never know...",
            "time_created": 1361043626,
            "rating_image_url": "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/f1def11e4e79/ico/stars/v1/stars_5.png",
            "rating_image_small_url": "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/c7623205d5cd/ico/stars/v1/stars_small_5.png",
            "user": {
                "image_url": "http://s3-media1.ak.yelpcdn.com/photo/58coTtu1x5riHSgFEAQsfw/ms.jpg",
                "id": "kVrW3138d5VL-AZ97wFF4A",
                "name": "Jeanne M."
            },
            "rating_image_large_url": "http://s3-media3.ak.yelpcdn.com/assets/2/www/img/22affc4e6c38/ico/stars/v1/stars_large_5.png",
            "id": "r5WtlQVMXiIMBR6S3N7RZw"
        }
    ],
    "phone": "9517870227",
    "snippet_text": "Dennis and his team did an amazing job on the roof of my fiancee's 2002 Acura RSX after years of living by the beach in San Francisco had mostly rusted...",
    "image_url": "http://s3-media3.ak.yelpcdn.com/bphoto/kodoEcmgHRG61pPaWRndbw/ms.jpg",
    "categories": [
        [
            "Body Shops",
            "bodyshops"
        ],
        [
            "Auto Repair",
            "autorepair"
        ]
    ],
    "display_phone": "+1-951-787-0227",
    "rating_img_url_large": "http://s3-media4.ak.yelpcdn.com/assets/2/www/img/9f83790ff7f6/ico/stars/v1/stars_large_4_half.png",
    "id": "economy-paint-and-collision-riverside",
    "is_closed": false,
    "location": {
        "city": "Riverside",
        "display_address": [
            "2548 Rubidoux Blvd",
            "Riverside, CA 92509"
        ],
        "geo_accuracy": 8,
        "postal_code": "92509",
        "country_code": "US",
        "address": [
            "2548 Rubidoux Blvd"
        ],
        "coordinate": {
            "latitude": 34.0132437,
            "longitude": -117.3923804
        },
        "state_code": "CA"
    }
}

You are probably having trouble because reviews is an array and you are trying to access it as a JSON object. 您可能遇到了问题,因为评论是一个数组,并且您尝试将其作为JSON对象进行访问。

$obj = json_decode($data, TRUE);
for($i=0; $i<count($obj['reviews']); $i++) {
    echo "Rating is " . $obj['reviews'][$i]["rating"] . " and the excerpt is " . $obj['reviews'][$i]["excerpt"] . "<BR>";
}

I'm not sure what exactly you want but I guess you want print it just for debugging right now. 我不确定你到底想要什么,但我想你现在想打印它只是为了调试。 You can try with print_r($obj); 你可以试试print_r($obj); and var_dump($obj); var_dump($obj); - they must print something, especially var_dump() . - 他们必须打印一些东西,特别是var_dump() When you see the data, you can easily edit function a little bit, so you can do for instance print_r($obj->reviews) or print_r($obj['reviews']) , depending if $obj is object or array. 当您看到数据时,您可以轻松地编辑一点功能,因此您可以执行print_r($obj->reviews)print_r($obj['reviews']) ,具体取决于$obj是对象还是数组。

You can use var_dump or print_r. 您可以使用var_dump或print_r。

<?php 
$decodedJSON = json_decode($jsonData);

// Put everyting to the screen with var_dump;
var_dump($decodedJSON);

// With print_r ( useful for arrays );
print_r($decodedJSON);

// List just review ratings with foreach;
foreach($decodedJSON['reviews'] as $review){
    echo $review['rating'];
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM