【问题标题】:Loop through parsed json string in php在 php 中循环遍历解析的 json 字符串
【发布时间】:2011-05-12 13:16:16
【问题描述】:

我有一个JSON格式的URL返回的复杂结构,我得到了可以通过var_dump查看的响应,现在我有了这个响应,

{
  "groups": [],
  "total_pages": 1,
  "spots": [
    {
      "address": {
        "region": "TX",
        "locality": "Austin"
      },
      "name": "Dirty Bill's",
      "checkins_count": 646,
      "_image_url_200": "http://static.gowalla.com/categories/28-b0d41920d32839ce1ecd6641e5fc2c87-200.png",
      "image_url": "http://static.gowalla.com/categories/28-78c9b4d7d239784df49dc932f64a3519-100.png",
      "_image_url_50": "http://static.gowalla.com/categories/28-78c9b4d7d239784df49dc932f64a3519-100.png",
      "radius_meters": 50,
      "trending_level": 0,
      "users_count": 375,
      "url": "/spots/43711",
      "checkins_url": "/checkins?spot_id=43711",
      "lng": "-97.7495040214",
      "spot_categories": [
        {
          "name": "Dive Bar",
          "url": "/categories/28"
        }
      ],
      "foursquare_id": null,
      "highlights_url": "/spots/43711/highlights",
      "items_url": "/spots/43711/items",
      "items_count": 11,
      "strict_radius": false,
      "description": "AKA the Gnome Bar. Much Warmer than Key Bar.",
      "activity_url": "/spots/43711/events",
      "lat": "30.2696322356",
      "photos_count": 23
    },
    {
      "address": {
        "region": "TX",
        "locality": "Austin"
      },
      "name": "Austin Wellness Clinic",
      "checkins_count": 1,
      "_image_url_200": "http://static.gowalla.com/categories/118-b41c2ba96f1ffe99fc23f12f0ee3b960-200.png",
      "image_url": "http://static.gowalla.com/categories/118-5f9e72162abf3dcbc0108cdbdba6a29f-100.png",
      "_image_url_50": "http://static.gowalla.com/categories/118-5f9e72162abf3dcbc0108cdbdba6a29f-100.png",
      "radius_meters": 75,
      "trending_level": 0,
      "users_count": 1,
      "url": "/spots/7360448",
      "checkins_url": "/checkins?spot_id=7360448",
      "lng": "-97.7495133877",
      "spot_categories": [
        {
          "name": "Health & Fitness",
          "url": "/categories/118"
        }
      ],
      "foursquare_id": null,
      "highlights_url": "/spots/7360448/highlights",
      "items_url": "/spots/7360448/items",
      "items_count": 0,
      "strict_radius": false,
      "description": null,
      "activity_url": "/spots/7360448/events",
      "lat": "30.2695755256",
      "photos_count": 0
    },

我已经使用 json_decode($response,true) 来获取解析变量,现在我不确定如何循环遍历它。有什么想法吗?!

编辑1:点是一个数组[],它的索引为0。我想遍历点数组中的每个名称值对

【问题讨论】:

标签: php arrays json


【解决方案1】:
<?php

$json = json_encode(
    array(
        'spots' => array(
            'bar' => 'baz',
            array(
                'quz' => 'foo',
                'bar' => 'baz'
            )
        )
    )
);

$root = json_decode( $json, true );

function read( $array ) {
    foreach( (array) $array as $key => $value ) {
        if( is_array( $value ) ) {
            read( $array );
        }
        echo "$key = $value\n";
    }
}

foreach( $root['spots'] as $spot ) {
    read( $spot );
}

这应该会为您提供点数组中的所有信息。

编辑:现在经过实际检查的语法,它可以工作了。

【讨论】:

  • 嗨。是的,抱歉,foreach 错过了一个 },所以它出现了。无论如何,它现在可以工作了。
【解决方案2】:
$result = json_decode($response,true);

foreach($result['spots'] as $spot)
{
    echo $spot['address']['locality'];
}

【讨论】:

    【解决方案3】:

    或者试试这个:

    $result = json_decode($response,true); $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($result)); foreach($iterator as $key=>$value) { echo "".$key."
    ".$value."
    "; }

    这会将复杂的关联数组转换为简单数组以进行循环。
    希望这会有所帮助。

    【讨论】:

      【解决方案4】:

      你不会循环通过它,你会使用recursion 来通过它。您需要编写一个可以调用自身的函数,将该结构的不同分支作为参数传递给自身。 This 可能会帮助您进行 PHP 递归。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-19
        • 2015-09-21
        • 1970-01-01
        • 1970-01-01
        • 2016-01-24
        • 2021-10-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多