簡體   English   中英

在PHP中解析JSON中的嵌套數組

[英]Parsing nested array in a JSON in PHP

我有以下帶有嵌套結構的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";
                                                }
                                }
                }

  }

?>

我需要從我的JSON中提取“ num *”值。

我的代碼工作正常,但似乎嵌套的“ for”循環可能不是從JSON中提取“ num *”值的最有效方法。

替代品或樣品?

您可以使用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);

看到它在eval.in上運行

使用這篇文章中的建議。 PHP多維數組按值搜索

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM