循环键下面的嵌套数组

时间:2013-05-14 10:47:40

标签: php arrays multidimensional-array foreach nested

好的,所以标题可能有点误导,但我不太清楚我在描述什么,所以这里就是。

我有以下JSON:

{
  "lastModified": 1368517749000,
  "name": "Requiem Paradisum",
  "realm": "Chamber of Aspects",
  "battlegroup": "Misery",
  "level": 25,
  "side": 1,
  "achievementPoints": 1710,
  "emblem": {
    "icon": 126,
    "iconColor": "ffdfa55a",
    "border": 3,
    "borderColor": "ff0f1415",
    "backgroundColor": "ff232323"
  },
  "news": [
    {
      "type": "itemPurchase",
      "character": "Osmoses",
      "timestamp": 1368482100000,
      "itemId": 91781
    },
    {
      "type": "itemLoot",
      "character": "Greenmean",
      "timestamp": 1368477900000,
      "itemId": 87209
    },
    {
      "type": "itemLoot",
      "character": "Greenmean",
      "timestamp": 1368475800000,
      "itemId": 86880
    },
    {
      "type": "itemPurchase",
      "character": "Osmoses",
      "timestamp": 1368475380000,
      "itemId": 91781
    },
    {
      "type": "itemPurchase",
      "character": "Osmoses",
      "timestamp": 1368475380000,
      "itemId": 91779
    },
    {
      "type": "itemPurchase",
      "character": "Osmoses",
      "timestamp": 1368475320000,
      "itemId": 91779
    },
    {
      "type": "playerAchievement",
      "character": "Osmoses",
      "timestamp": 1368470700000,
      "achievement": {
        "id": 6193,
        "title": "Level 90",
        "points": 10,
        "description": "Reach level 90.",
        "rewardItems": [
          {
            "id": 87764,
            "name": "Serpent's Heart Firework",
            "icon": "inv_misc_missilelarge_green",
            "quality": 1,
            "itemLevel": 1,
            "tooltipParams": {

            },
            "stats": [

            ],
            "armor": 0
          }
        ],
        "icon": "achievement_level_90",
        "criteria": [

        ],
        "accountWide": false,
        "factionId": 2
      }
    },

基本上我需要循环“新闻”中的所有内容并输出它。 我无法弄清楚如何正确解析它: 答:没有指定密钥和 B:当它到达一个密钥然后包含更多密钥和那些密钥下的其他数组时我不知所措。 (例如“玩家成就”键) 我很欣赏我可能在这里有点新手,很可能在“php for dummies”的第1页上,但我发誓我已经用谷歌搜索它了!

3 个答案:

答案 0 :(得分:1)

json_decode

别忘了给第二个参数TRUE,否则会返回对象

尝试这样的事情

 $json         = 'your json'
 $json_array   = json_decode($json,true);

 $news         = $json_array['news'];

foreach($news as $value)
{

     print_r($value);

}

我认为你应该看看array_walk_recursive

  function printResult($item, $key)
  {
     echo "$key holds $item\n";
  }
  array_walk_recursive($news, 'printResult');

答案 1 :(得分:1)

看看这种方法是否合适。请确保您的JSON数组格式正确。

 $test = the_json_array;    
 $array = json_decode($test,true);
  function recursive($array) {
        foreach($array as $key => $value) {
            if (!is_array($value)) echo $key.":".$value."<br/>";
            else recursive($value);
        }
      }
 recursive($array);

答案 2 :(得分:0)

你需要做json_decode('your-jason', True)它会将你的Json字符串转换为数组。

Json_decode for more understanding

如果你没有在TRUE函数中指定jason_decode那么它将返回php对象

希望回答这个问题。问候