从多维数组构建新的多维数组并解释顺序

时间:2014-10-04 07:54:56

标签: php arrays multidimensional-array

我一直试图让它工作7.5小时,但我的大脑已经融化了。

我有一个类似于以下内容的多维数组:

array (
  'expanded' => true,
  'key' => 'root_1',
  'title' => 'root',
  'children' => 
  array (
    0 => 
    array (
      'folder' => false,
      'key' => '_1',
      'title' => 'News',
      'data' => 
      array (
        'id' => '3',
        'parent_id' => 0,
      ),
    ),
    1 => 
    array (
      'folder' => true,
      'key' => '_2',
      'title' => 'Animations',
      'data' => 
      array (
        'id' => '5',
        'parent_id' => '0',
      ),
      'children' => 
      array (
        0 => 
        array (
          'folder' => false,
          'key' => '_3',
          'title' => 'The Simpsons',
          'data' => 
          array (
            'id' => '1',
            'parent_id' => '5',
          ),
        ),
        1 => 
        array (
          'folder' => false,
          'key' => '_4',
          'title' => 'Futurama',
          'data' => 
          array (
            'id' => '4',
            'parent_id' => '5',
          ),
        ),
      ),
    ),
    2 => 
    array (
      'folder' => true,
      'key' => '_5',
      'title' => 'Episodes',
      'data' => 
      array (
        'id' => '6',
        'parent_id' => '0',
      ),
      'children' => 
      array (
        0 => 
        array (
          'folder' => true,
          'key' => '_6',
          'title' => 'UK Episodes',
          'data' => 
          array (
            'id' => '7',
            'parent_id' => '6',
          ),
          'children' => 
          array (
            0 => 
            array (
              'folder' => false,
              'key' => '_7',
              'title' => 'Coupling',
              'data' => 
              array (
                'id' => '2',
                'parent_id' => '7',
              ),
            ),
          ),
        ),
        1 => 
        array (
          'folder' => true,
          'key' => '_8',
          'title' => 'AU Episodes',
          'data' => 
          array (
            'id' => '8',
            'parent_id' => '6',
          ),
        ),
      ),
    ),
  ),
)

我需要搜索所有子数组并构建一个新数组,返回每个子节点的id和parent_id,并根据它们在原始数组中出现的顺序解释数组子节点的顺序。

输出需要最终结果如下:

Array
(
    [0] => Array
        (
            [id] => 1
            [parent_id] => 5
            [order] => 1
        )

    [1] => Array
        (
            [id] => 2
            [parent_id] => 7
            [order] => 1
        )

    [2] => Array
        (
            [id] => 4
            [parent_id] => 5
            [order] => 2
        )
)

我尝试过以下recursion approach,但是我的代码变得一团糟,而在我甚至试图设置项目的顺序之前。

我还搜索了stackoverflow,这是另一个我可以学习的例子但是我还没有找到任何东西......如果有另一个例子,请随意指出我正确的方向。

我感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

运行所有这些操作的一种非常简单的方法是在RecursiveIteratorIterator上使用RecursiveArrayIterator,取出'data'数组并按顺序添加密钥。


实施例

$array = []; // Your starting array with stuff in it.

$iterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator($array),
    RecursiveIteratorIterator::SELF_FIRST
);
$result = [];
foreach ($iterator as $key => $value) {
    if (isset($value['data'])) {
        $result[] = array_merge($value['data'], ['order' => $key]);
    }
}

print_r($result);

输出:

Array
(
    [0] => Array
        (
            [id] => 3
            [parent_id] => 0
            [order] => 0
        )

    [1] => Array
        (
            [id] => 5
            [parent_id] => 0
            [order] => 1
        )

    [2] => Array
        (
            [id] => 1
            [parent_id] => 5
            [order] => 0
        )

    [3] => Array
        (
            [id] => 4
            [parent_id] => 5
            [order] => 1
        )

    [4] => Array
        (
            [id] => 6
            [parent_id] => 0
            [order] => 2
        )

    [5] => Array
        (
            [id] => 7
            [parent_id] => 6
            [order] => 0
        )

    [6] => Array
        (
            [id] => 2
            [parent_id] => 7
            [order] => 0
        )

    [7] => Array
        (
            [id] => 8
            [parent_id] => 6
            [order] => 1
        )

)

答案 1 :(得分:0)

如果我理解你正在尝试做什么,下面的代码片段应该可行。不需要每个子数组中的顺序键,因为当您将新的数组索引插入$new_array时会自动记录。

如果您有任何其他问题,请告诉我。

$new_array = [];

$current_array = []; //with all your data

foreach($current_array['children'] as $array) {
    $new_array[] = [
        'id' => $array['data']['id'],
        'parent_id' => $array['data']['parent_id'];
    ];
}