简体   繁体   English

在PHP中解析JSON中的嵌套数组

[英]Parsing nested array in a JSON in PHP

I've the following PHP code with nested structures 我有以下带有嵌套结构的PHP代码

<?php


$theJson = <<<EOF
{
  "date": 1492804820000,
  "node1": [{
    "id": 57163,
    "node1_description": "The node1 description",
    "node2": [{
      "id": 57163,
      "node2_description": "The node2 description",
      "dipartments": [{
        "id": 57163,
        "dip_description": "The dipartment description",
        "colorCods": [{
                    "id": 1,
                    "description": "Red",
                    "numbers": {
                        "num1": 1,
            "num2": 23,
            "num3": 11
                    }
                }, {
          "id": 2,
                    "description": "Yellow",
                    "numbers": {
                        "num1": 3,
            "num2": 45,
            "num3": 33
                    }
                }, {
          "id": 3,
                    "description": "Green",
                    "numbers": {
                        "num1": 31,
            "num2": 453,
            "num3": 323
                    }
                }, {
          "id": 3,
                    "description": "White",
                    "numbers": {
                        "num1": 3,
            "num2": 4,
            "num3": 3
                    }
                }]
      },
      {
        "id": 57345,
        "dip_description": "The dipartment description 2",
        "colorCods": [{
          "id": 1,
          "description": "Red",
          "numbers": {
            "num1": 4,
            "num2": 243,
            "num3": 151
          }
        }, {
          "id": 2,
          "description": "Yellow",
          "numbers": {
            "num1": 33,
            "num2": 415,
            "num3": 3
          }
        }, {
          "id": 3,
          "description": "Green",
          "numbers": {
            "num1": 331,
            "num2": 43,
            "num3": 33
          }
        }, {
          "id": 3,
          "description": "White",
          "numbers": {
            "num1": 32,
            "num2": 42,
            "num3": 33
          }
        }]
      }
    ]
    }]
  }]
}
EOF;


  $theArray = json_decode($theJson, true);

  foreach ($theArray['node1'] as $node1) {
          echo "Node 1 description :".$node1['node1_description'];
          echo "\n";
          echo "\n";
                foreach ($node1['node2'] as $node2) {
                        echo "Node 2 description :".$node2['node2_description'];
                        echo "\n";
                        echo "\n";
                                foreach ($node2['dipartments'] as $dip) {
                                                echo "Dipartment description: ".$dip['dip_description'];
                                                echo "\n";
                                                echo "\n";
                                                foreach ($dip['colorCods'] as $colCod) {
                                                          echo "Cod: ".$colCod['description'];
                                                          echo " - ";
                                                                echo "Number: ".$colCod['numbers']['num1'];
                                                                echo "\n";
                                                                echo "\n";
                                                }
                                }
                }

  }

?>

I need to extract the "num*" values from my JSON. 我需要从我的JSON中提取“ num *”值。

My code works fine, but it seems that the nested "for" cycles might not be the most efficient way to extract the "num*" values from my JSON. 我的代码工作正常,但似乎嵌套的“ for”循环可能不是从JSON中提取“ num *”值的最有效方法。

Alternatives or samples? 替代品或样品?

You could use array_walk_recursive and then filter for the keys whose values you want to collect: 您可以使用array_walk_recursive ,然后过滤要收集其值的键:

$result = [];
array_walk_recursive($theArray, function ($value, $key) use (&$result) {
    if (preg_match('/^num[0-9]+$/', $key)) {
        $result[] = $value;
    }
});

print_r($result);

See it run on eval.in 看到它在eval.in上运行

Use recommendations from this post. 使用这篇文章中的建议。 PHP multidimensional array search by value PHP多维数组按值搜索

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

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