沃梦达 / 编程问答 / php问题 / 正文

循环遍历键下面的嵌套数组

Looping over nested arrays below keys(循环遍历键下面的嵌套数组)

本文介绍了循环遍历键下面的嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这个标题可能有点误导,但我不太确定我在描述什么,所以我们这样做。

我有以下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
      }
    },

基本上我需要循环"news"中的所有内容并输出它。 我不知道如何正确解析它: 答:不指定按键号和 B:当它到达包含更多键和这些键下的更多数组的键时,我就不知所措了。(例如"玩家成就"键) 我承认我在这里可能是个新手,很可能在"假人的php"的第一页,但我发誓我已经用谷歌搜索死了!

推荐答案

查看此方法是否适合。不过,请确保您的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);

这篇关于循环遍历键下面的嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:循环遍历键下面的嵌套数组

基础教程推荐