当前位置: 首页 > 面试题库 >

如何使用PHP遍历JSON数组

马沛
2023-03-14
问题内容

我的PHP代码:

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

虽然它适用于非数组,但我终生无法弄清楚如何打印“审阅”数组中的所有值。

我想做的是遍历此响应,可能使用forreach(),从而得到一个包含响应中每个评论的评分和摘要的列表。

任何指导/方向将不胜感激。

以下是我正在使用的JSON。(这是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"
    }
}

问题答案:

您可能会遇到麻烦,因为评论是一个数组,并且您试图将其作为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>";
}


 类似资料:
  • 问题内容: 我有一个json数组: 如何使用php遍历此数组? 问题答案: 如果需要关联数组,请将第二个函数参数设置为true 如果您需要关联数组,则某些版本的php需要true的第二个参数

  • 问题内容: 如何使用CodeIgniter在以下JSON中遍历并显示名称? 问题答案: 1)是一个字符串,您需要首先对其进行解码。 2)您需要遍历对象并获取其成员

  • 问题内容: 我有一些带有多个对象的JSON代码: 我的JSON循环代码段是: 您能否让我知道如何检查阵列中是否没有“ MNGR_NAME”。(以我为例,它出现了两次。) 问题答案: 您需要在迭代时访问对象。

  • 问题内容: 我有一个可以用SwiftyJSON解析的json: 完美运作。 但是我无法遍历它。我尝试了两种方法,第一种是 XCode不接受for循环声明。 第二种方法: XCode不接受if语句。 我究竟做错了什么 ? 问题答案: 如果要遍历数组,请尝试: 至于第二种方法,返回 非 数组,应该使用:

  • 问题内容: 我正在尝试使用Powershell访问JSON对象的特定属性值。不幸的是,我不知道结构中某些父级属性的键,因此我无法直接做到这一点。另外,由于JSON不是数组,因此无法通过索引位置访问。 上下文是我正在从elasticsearch查询正在运行的任务列表,并且需要获取任务的ID(我知道只有一个),因此我可以进行后续调用以发现其完成状态。 我已经研究了一些查询方法,但是不确定如何应用它们(

  • 问题内容: 我有一个需要迭代的JSON文件,如下所示… 数据中的键并不总是相同的(我只是使用示例,有20个不同的键),因此,我无法设置脚本来静态引用它们以获取值。 否则我可以说 过去我在数据节点上使用了一个简单的foreach循环… 但是不想阻止脚本。有任何想法吗? 问题答案: 您可以通过以下方式遍历JavaScript对象: myobject可能是您的json.data