JSONの全要素を再帰的に取得してパースするコード[PHP]

  • このエントリーをはてなブックマークに追加
  • Pocket

階層の深さが不明な多次元連想配列(JSONオブジェクト)のネストされた全てのノードにアクセスします。

ツリー情報を要素ごとに再帰的に取得し、全ての要素の親子関係を網羅するサンプルコードを掲載します。

コード

サンプルとして下記のJSONを用います。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"Description": "SampleJson",
"Copyright": "Copyright (C) 2018 mio.yokohama All Rights Reserved.",
"Status": "200",
"CompressType": "",
"Feature": [
{
"Id": "0106001",
"Name": "うーメン",
"Property": {
"first": {
"second": {
"third": {
"target": "3rd",
"fifth": {
"sixth": {
"Target": "6th"
},
"Target": "5th"
}
}
},
"Target": "1st"
}
}
}
],
"Dictionary": {
"Genre": [
{
"Id": "01",
"Name": "土えもの付属うーメン",
"bool": "True",
"Level": 1
},
{
"Id": "0106",
"Name": "ビーノレ",
"bool": false,
"Level": 2
}
]
}
}
{ "Description": "SampleJson", "Copyright": "Copyright (C) 2018 mio.yokohama All Rights Reserved.", "Status": "200", "CompressType": "", "Feature": [ { "Id": "0106001", "Name": "うーメン", "Property": { "first": { "second": { "third": { "target": "3rd", "fifth": { "sixth": { "Target": "6th" }, "Target": "5th" } } }, "Target": "1st" } } } ], "Dictionary": { "Genre": [ { "Id": "01", "Name": "土えもの付属うーメン", "bool": "True", "Level": 1 }, { "Id": "0106", "Name": "ビーノレ", "bool": false, "Level": 2 } ] } }
{
  "Description": "SampleJson",
  "Copyright": "Copyright (C) 2018 mio.yokohama All Rights Reserved.",
  "Status": "200",
  "CompressType": "",
  "Feature": [
    {
      "Id": "0106001",
      "Name": "うーメン",
      "Property": {
        "first": {
          "second": {
            "third": {
              "target": "3rd",
              "fifth": {
                "sixth": {
                  "Target": "6th"
                },
                "Target": "5th"
              }
            }
          },
          "Target": "1st"
        }
      }
    }
  ],
  "Dictionary": {
    "Genre": [
      {
        "Id": "01",
        "Name": "土えもの付属うーメン",
        "bool": "True",
        "Level": 1
      },
      {
        "Id": "0106",
        "Name": "ビーノレ",
        "bool": false,
        "Level": 2
      }
    ]
  }
}

サンプル(親子関係網羅)

末端要素とその全ての親を再帰的に処理するコードを書きました。
サンプルでは標準出力しています。

出力結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Description|SampleJson
Copyright|Copyright (C) 2018 mio.yokohama All Rights Reserved.
Status|200
CompressType|
Feature|0|Id|0106001
Feature|0|Name|うーメン
Feature|0|Property|first|second|third|target|3rd
Feature|0|Property|first|second|third|fifth|sixth|Target|6th
Feature|0|Property|first|second|third|fifth|Target|5th
Feature|0|Property|first|Target|1st
Dictionary|Genre|0|Id|01
Dictionary|Genre|0|Name|土えもの付属うーメン
Dictionary|Genre|0|bool|True
Dictionary|Genre|0|Level|1
Dictionary|Genre|1|Id|0106
Dictionary|Genre|1|Name|ビーノレ
Dictionary|Genre|1|bool|false
Dictionary|Genre|1|Level|2
Description|SampleJson Copyright|Copyright (C) 2018 mio.yokohama All Rights Reserved. Status|200 CompressType| Feature|0|Id|0106001 Feature|0|Name|うーメン Feature|0|Property|first|second|third|target|3rd Feature|0|Property|first|second|third|fifth|sixth|Target|6th Feature|0|Property|first|second|third|fifth|Target|5th Feature|0|Property|first|Target|1st Dictionary|Genre|0|Id|01 Dictionary|Genre|0|Name|土えもの付属うーメン Dictionary|Genre|0|bool|True Dictionary|Genre|0|Level|1 Dictionary|Genre|1|Id|0106 Dictionary|Genre|1|Name|ビーノレ Dictionary|Genre|1|bool|false Dictionary|Genre|1|Level|2
Description|SampleJson
Copyright|Copyright (C) 2018 mio.yokohama All Rights Reserved.
Status|200
CompressType|
Feature|0|Id|0106001
Feature|0|Name|うーメン
Feature|0|Property|first|second|third|target|3rd
Feature|0|Property|first|second|third|fifth|sixth|Target|6th
Feature|0|Property|first|second|third|fifth|Target|5th
Feature|0|Property|first|Target|1st
Dictionary|Genre|0|Id|01
Dictionary|Genre|0|Name|土えもの付属うーメン
Dictionary|Genre|0|bool|True
Dictionary|Genre|0|Level|1
Dictionary|Genre|1|Id|0106
Dictionary|Genre|1|Name|ビーノレ
Dictionary|Genre|1|bool|false
Dictionary|Genre|1|Level|2

コード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
$json_string = '〜省略(JsonStrings)〜';
$json_array = json_decode($json_string, true);
getArrayElementsRecursively($json_array);
function getArrayElementsRecursively($array, $depth = 0, $index = array(), $strings = "") {
foreach ((array)$array as $key => $value) {
for ($i = 0;$i < $depth;$i++) $strings.= $index[$i] . "|";
if (!is_array($key)) {
$index[$depth] = $key;
$strings.= $key . "|";
}
if (is_array($value)) {
$strings = "";
$depth++;
getArrayElementsRecursively($value, $depth, $index, $strings);
$depth--;
} else {
if (is_bool($value)) $value = ((true === $value) ? 'true' : 'false');
$strings.= strval($value) . PHP_EOL;
}
echo $strings;
$strings = "";
}
}
?>
<?php $json_string = '〜省略(JsonStrings)〜'; $json_array = json_decode($json_string, true); getArrayElementsRecursively($json_array); function getArrayElementsRecursively($array, $depth = 0, $index = array(), $strings = "") { foreach ((array)$array as $key => $value) { for ($i = 0;$i < $depth;$i++) $strings.= $index[$i] . "|"; if (!is_array($key)) { $index[$depth] = $key; $strings.= $key . "|"; } if (is_array($value)) { $strings = ""; $depth++; getArrayElementsRecursively($value, $depth, $index, $strings); $depth--; } else { if (is_bool($value)) $value = ((true === $value) ? 'true' : 'false'); $strings.= strval($value) . PHP_EOL; } echo $strings; $strings = ""; } } ?>
<?php
$json_string = '〜省略(JsonStrings)〜';
$json_array = json_decode($json_string, true);
getArrayElementsRecursively($json_array);
function getArrayElementsRecursively($array, $depth = 0, $index = array(), $strings = "") {
    foreach ((array)$array as $key => $value) {
        for ($i = 0;$i < $depth;$i++) $strings.= $index[$i] . "|";
        if (!is_array($key)) {
            $index[$depth] = $key;
            $strings.= $key . "|";
        }
        if (is_array($value)) {
            $strings = "";
            $depth++;
            getArrayElementsRecursively($value, $depth, $index, $strings);
            $depth--;
        } else {
            if (is_bool($value)) $value = ((true === $value) ? 'true' : 'false');
            $strings.= strval($value) . PHP_EOL;
        }
        echo $strings;
        $strings = "";
    }
} 
?>
  • このエントリーをはてなブックマークに追加
  • Pocket

SNSでもご購読できます。

コメントを残す

*

CAPTCHA