2017-06-22 16 views
-1

このコードを持っている:PHP - 配列 - 再帰のXpathとしてネストされた配列(木)の(リーフ/葉まで)各フルブランチを印刷

$text = '{"token_name":"C_ROOT","token_group":"C_BLOCK","group":true,"body":[[{"token_name_org":"T_VARIABLE","token":320,"value":"sort","line":2,"token_group":"VARIABLES","token_name":"C_VARIABLE"},{"token_name_org":"C_ASSIGNMENT_EQUAL","line":2,"value":"=","token":"VALUE","token_group":"ASSIGNMENTS"},{"token_name_org":"T_VARIABLE","token":320,"value":"_GET","line":2,"token_group":"VARIABLES","token_name":"C_VARIABLE","args":[[{"token_name_org":"T_CONSTANT_ENCAPSED_STRING","token":323,"value":"sort","line":2,"token_group":"STRINGS","token_name":"C_STRING"}]]}],[{"token_name_org":"T_VARIABLE","token":320,"value":"mort","line":2,"token_group":"VARIABLES","token_name":"C_VARIABLE"},{"token_name_org":"C_ASSIGNMENT_EQUAL","line":2,"value":"=","token":"VALUE","token_group":"ASSIGNMENTS"},{"token_name_org":"T_VARIABLE","token":320,"value":"_GET","line":2,"token_group":"VARIABLES","token_name":"C_VARIABLE","args":[[{"token_name_org":"T_CONSTANT_ENCAPSED_STRING","token":323,"value":"mort","line":2,"token_group":"STRINGS","token_name":"C_STRING"}]]}],[{"token_name_org":"T_VARIABLE","token":320,"value":"bort","line":2,"token_group":"VARIABLES","token_name":"C_VARIABLE"},{"token_name_org":"C_ASSIGNMENT_EQUAL","line":2,"value":"=","token":"VALUE","token_group":"ASSIGNMENTS"},{"token_name_org":"T_VARIABLE","token":320,"value":"_GET","line":2,"token_group":"VARIABLES","token_name":"C_VARIABLE","args":[[{"token_name_org":"T_CONSTANT_ENCAPSED_STRING","token":323,"value":"bort","line":2,"token_group":"STRINGS","token_name":"C_STRING"}]]}]]}'; 

$array = json_decode($text, TRUE); 


// Collect the values here. 
// Start with an empty string to force a leading '/' in the output 
$path = array(''); 
// Walk the array, put the desired values in $path 
array_walk_recursive(
    $array, 
    function($value, $key) use (&$path) {  // use reference to modify $path inside the function 
     if ($key == 'value') { 
      $path[] = $value; 
     } 
    } 
); 

// Join the collected values and output the result 
echo(implode('/', $path)); 

をそれはアレイ(木)と印刷物を通過葉までの各ブランチのXPath、:JSONとして

清書配列:

{ 
    "token_name": "C_ROOT", 
    "token_group": "C_BLOCK", 
    "group": true, 
    "body": [ 
    [ 
     { 
     "token_name_org": "T_VARIABLE", 
     "token": 320, 
     "value": "sort", 
     "line": 2, 
     "token_group": "VARIABLES", 
     "token_name": "C_VARIABLE" 
     }, 
     { 
     "token_name_org": "C_ASSIGNMENT_EQUAL", 
     "line": 2, 
     "value": "=", 
     "token": "VALUE", 
     "token_group": "ASSIGNMENTS" 
     }, 
     { 
     "token_name_org": "T_VARIABLE", 
     "token": 320, 
     "value": "_GET", 
     "line": 2, 
     "token_group": "VARIABLES", 
     "token_name": "C_VARIABLE", 
     "args": [ 
      [ 
      { 
       "token_name_org": "T_CONSTANT_ENCAPSED_STRING", 
       "token": 323, 
       "value": "sort", 
       "line": 2, 
       "token_group": "STRINGS", 
       "token_name": "C_STRING" 
      } 
      ] 
     ] 
     } 
    ], 
    [ 
     { 
     "token_name_org": "T_VARIABLE", 
     "token": 320, 
     "value": "mort", 
     "line": 2, 
     "token_group": "VARIABLES", 
     "token_name": "C_VARIABLE" 
     }, 
     { 
     "token_name_org": "C_ASSIGNMENT_EQUAL", 
     "line": 2, 
     "value": "=", 
     "token": "VALUE", 
     "token_group": "ASSIGNMENTS" 
     }, 
     { 
     "token_name_org": "T_VARIABLE", 
     "token": 320, 
     "value": "_GET", 
     "line": 2, 
     "token_group": "VARIABLES", 
     "token_name": "C_VARIABLE", 
     "args": [ 
      [ 
      { 
       "token_name_org": "T_CONSTANT_ENCAPSED_STRING", 
       "token": 323, 
       "value": "mort", 
       "line": 2, 
       "token_group": "STRINGS", 
       "token_name": "C_STRING" 
      } 
      ] 
     ] 
     } 
    ], 
    [ 
     { 
     "token_name_org": "T_VARIABLE", 
     "token": 320, 
     "value": "bort", 
     "line": 2, 
     "token_group": "VARIABLES", 
     "token_name": "C_VARIABLE" 
     }, 
     { 
     "token_name_org": "C_ASSIGNMENT_EQUAL", 
     "line": 2, 
     "value": "=", 
     "token": "VALUE", 
     "token_group": "ASSIGNMENTS" 
     }, 
     { 
     "token_name_org": "T_VARIABLE", 
     "token": 320, 
     "value": "_GET", 
     "line": 2, 
     "token_group": "VARIABLES", 
     "token_name": "C_VARIABLE", 
     "args": [ 
      [ 
      { 
       "token_name_org": "T_CONSTANT_ENCAPSED_STRING", 
       "token": 323, 
       "value": "bort", 
       "line": 2, 
       "token_group": "STRINGS", 
       "token_name": "C_STRING" 
      } 
      ] 
     ] 
     } 
    ] 
    ] 
} 

出力:

/sort/=/_GET/sort/mort/=/_GET/mort/bort/=/_GET/bort 

誰かが、入れ子配列の数をどのように数えることができますか、いつ停止するか、分岐がどこで終了するかを知っていますか?また、 "ソート"だけを検索するように指定するには、 "mort"と "bort"の枝を表示しないでください。

出力は、検索「並べ替え」に基づいて、次のようになります。

/sort/=/_GET/sort 

だから、擬似コードは、私はこのようなものだと思うようになります。

  1. は、キー「の値を持つ「身体」のアレイ内を検索します"検索と同じ(つまり、"ソート ")
  2. ブランチが終了するまで、すべての兄弟や子供、子供の子供などのキーの" value "値を取得します。
  3. 印刷枝次のボディアイテム

ありがとう

  • 後藤1、

  • 答えて

    1

    まずあなたがvalue == 'sort'は、既存のコードを適用含まbodyの項目を識別しなければならない(関数の中でそれを抽出最初):

    $search = 'sort'; 
    
    // Identify the items having 'value' associated with $search 
    $allItems = array_filter(
        $array['body'], 
        function (array $item) use ($search) { 
         // $item contains many tokens, keep $item if the first token has value == 'sort' 
         // just to be sure you can also check here if $item[0]['token'] is 320 or the value of 'token_org_name' 
         return $item[0]['value'] == $search; 
        } 
    ); 
    
    // Pass all the found items to the function that produces the path 
    foreach ($allItems as $item) { 
        echo(getPath($item)."\n"); 
    } 
    
    
    function getPath(array $array) { 
        // Collect the values here. 
        // Start with an empty string to force a leading '/' in the output 
        $path = array(''); 
        // Walk the array, put the desired values in $path 
        array_walk_recursive(
         $array, 
         function($value, $key) use (&$path) { 
          if ($key == 'value') { 
           $path[] = $value; 
          } 
         } 
        ); 
    
        // Join the collected values and return the result 
        return implode('/', $path); 
    } 
    
    +0

    大きな説明、コメントとコード。ありがとう!!! –