2016-12-28 5 views
0

私は保存された無制限の追加データを持つ入れ子レベルの配列を持っています。その後、同じ配列を再現し、私はすべてのレベルからlevelextra_keyなどの不要なキーを削除したいと私はまたnodesにキーchildrenの名前を変更したいPHPの再フォーマットネストされた配列

$arr = array(
    array(
     'name' => 'item1', 
     'nodes' => array(
      'name' => 'sub-item1', 
      'nodes' => array(
       'name' => 'sub-sub-item1', 
       'nodes' => array() 
      ), 
      array(
       'name' => 'sub-sub2-item1', 
       'nodes' => array() 
      ), 
     ) 
    ), 
    array(
     'name' => 'item2', 
     'nodes' => array(
      'name' => 'sub-item2', 
      'nodes' => array(
       'name' => 'sub-sub-item2', 
       'nodes' => array() 
      ), 
      array(
       'name' => 'sub-sub2-item2', 
       'nodes' => array() 
      ), 
     ) 
    ), 
    array(
     'name' => 'item3', 
     'nodes' => array(
      'name' => 'sub-item3', 
      'nodes' => array(
       'name' => 'sub-sub-item3', 
       'nodes' => array() 
      ), 
      array(
       'name' => 'sub-sub2-item3', 
       'nodes' => array() 
      ), 
     ) 
    ) 
); 

$arr = array(
    array(
     'name' => 'item1', 
     'level' => 0, 
     'extra_key' => 'some_data', 
     'children' => array(
      'name' => 'sub-item1', 
      'level' => 1, 
      'extra_key' => 'some_data', 
      'children' => array(
       'name' => 'sub-sub-item1', 
       'level' => 2, 
       'extra_key' => 'some_data', 
       'children' => array() 
      ), 
      array(
       'name' => 'sub-sub2-item1', 
       'level' => 2, 
       'extra_key' => 'some_data', 
       'children' => array() 
      ), 
     ) 
    ), 
    array(
     'name' => 'item2', 
     'level' => 0, 
     'extra_key' => 'some_data', 
     'children' => array(
      'name' => 'sub-item2', 
      'level' => 2, 
      'extra_key' => 'some_data', 
      'children' => array(
       'name' => 'sub-sub-item2', 
       'level' => 2, 
       'extra_key' => 'some_data', 
       'children' => array() 
      ), 
      array(
       'name' => 'sub-sub2-item2', 
       'level' => 2, 
       'extra_key' => 'some_data', 
       'children' => array() 
      ), 
     ) 
    ), 
    array(
     'name' => 'item3', 
     'level' => 0, 
     'extra_key' => 'some_data', 
     'children' => array(
      'name' => 'sub-item3', 
      'level' => 1, 
      'extra_key' => 'some_data', 
      'children' => array(
       'name' => 'sub-sub-item3', 
       'level' => 2, 
       'extra_key' => 'some_data', 
       'children' => array() 
      ), 
      array(
       'name' => 'sub-sub2-item3', 
       'level' => 2, 
       'extra_key' => 'some_data', 
       'children' => array() 
      ), 
     ) 
    ), 
); 

予想される出力配列新しいフォーマットのと同じ構造を使用してください。

どうすれば実現できますか?

私は再帰関数でそれを実行しようとしましたが、私は

+0

は、あなたが私たちを見ることができるものは、あなたが出力として期待される配列になりphp.net –

+0

に本を読んarray_walk_recursive? – zsram

+0

@zsram質問を編集して、予想される出力配列を追加しました – semsem

答えて

1

あなたの構造は、あなたが再帰関数を記述することができませんでした、おそらくそのため、意味がありません同じstructrueを再現することができませんでした。構造を変更することができれば、私はこれを提案するでしょう(再フォーマット機能実装あり):

<?php 

$actual = array(
    array(
     'name' => 'item1', 
     'level' => 0, 
     'extra_key' => 'some_data', 
     'children' => array(
      array(
       'name' => 'sub-item1', 
       'level' => 1, 
       'extra_key' => 'some_data', 
       'children' => array(
        array(
         'name' => 'sub-sub-item1', 
         'level' => 2, 
         'extra_key' => 'some_data', 
         'children' => array() 
        ), 
        array(
         'name' => 'sub-sub2-item1', 
         'level' => 2, 
         'extra_key' => 'some_data', 
         'children' => array() 
        ), 
       ) 
      ), 
     ) 
    ), 
); 

$expected = array(
    array(
     'name' => 'item1', 
     'nodes' => array(
      array(
       'name' => 'sub-item1', 
       'nodes' => array(
        array(
         'name' => 'sub-sub-item1', 
         'nodes' => array() 
        ), 
        array(
         'name' => 'sub-sub2-item1', 
         'nodes' => array() 
        ), 
       ), 
      ), 
     ) 
    ), 
); 

function change_array($original) 
{ 
    return array_map('change_node', $original); 
} 

function change_node($node) 
{ 
    return [ 
     'name' => $node['name'], 
     'nodes' => array_map('change_node', $node['children']), 
    ]; 
} 

var_dump($expected === change_array($actual)); 
+0

ありがとう、完璧に働いた! – semsem

1

この関数は再帰的にしたいことを行います。 必要に応じて、除外されたインデックスおよび/または名前が変更されたインデックスを追加できます。

function parseArray($array): array 
{ 
    // The resulting array 
    $result   = []; 
    // Indices you want renamed [`from` => `to`] 
    $renameIndices = ['children' => 'nodes']; 
    // Indices you want excluded [`1`, `2`] 
    $excludedIndices = ['level', 'extra_key']; 
    foreach ($array as $idx => $content) 
    { 
     // If excluded, continue (skip) node. 
     if (in_array($idx, $excludedIndices, true)) 
     { 
      continue; 
     } 
     // Setting the resulting (new) index. 
     $resultIdx = $idx; 
     // Check if this index should be renamed 
     if (array_key_exists($idx, $renameIndices)) 
     { 
      // If index should be renamed, apply new name 
      $resultIdx = $renameIndices[$idx]; 
     } 
     // If this content block is an array. Parse it. 
     if (is_array($content)) 
     { 
      $content = parseArray($content); 
     } 
     // Save content to resulting array. 
     $result[$resultIdx] = $content; 
    } 

    return $result; 
} 
+0

ありがとう、 こちらをご覧ください@Anton Serdyuk repyここをクリックhttp://stackoverflow.com/a/41364381/1344637 – semsem