PHP:将多维数组拆分为数组?

时间:2013-11-30 16:41:51

标签: php arrays multidimensional-array

转换此多维数组的最佳方法是什么:

Array
(
    [0] => Array
        (
            [nome] => Livello1
            [id] => 47
            [level] => 0
            [0] => Array
                (
                    [nome] => Livello 2
                    [id] => 48
                    [level] => 1
                    [0] => Array
                        (
                            [nome] => Livello 3
                            [id] => 50
                            [level] => 2
                        )

                )

            [1] => Array
                (
                    [nome] => Livello2bis
                    [id] => 49
                    [level] => 1
                    [0] => Array
                        (
                            [nome] => Livello 3 Bis
                            [id] => 51
                            [level] => 2
                        )

                )

        )

    [1] => Array
        (
            [nome] => Livello 1 bis
            [id] => 52
            [level] => 0
        )

)

采用以下格式:

    Array
    (   
        [0] => Array
        (
            [nome] => Livello1
            [id] => 47
            [level] => 0
         )
        [1] => Array
            (
                [nome] => Livello 2
                [id] => 48
                [level] => 1
            )
        [2] => Array
             (
                 [nome] => Livello 3
                 [id] => 50
                 [level] => 2
             )

    ......
    )

1 个答案:

答案 0 :(得分:4)

您可以使用RecursiveIteratorIterator()(满口)迭代嵌套数组的叶子,使用RecursiveIteratorIterator::getDepth()为每个级别创建一个新数组,并将其添加到结果集中。例如:

<?php

// wrap the array in a recursive iterator and a recursive iterator iterator o_O
$arrayIterator = new RecursiveArrayIterator($array);
$iterator = new RecursiveIteratorIterator($arrayIterator);

// init an array to save results
$results = array();
// save inital depth, which is 0
$depth = $iterator->getDepth();
// init an array in which we'll save all leaves per level
$leaves = array();

// iterate
foreach ($iterator as $key => $leaf) {

    // if the depth has changed, we want 
    // to save the leaves array in results,
    // assign the new depth for comparison
    // on the next iteration
    // and reinit the leaves array
    if ($depth !== $iterator->getDepth()) {
        $depth = $iterator->getDepth();
        if (count($leaves) > 0) {
            $results[] = $leaves;
        }
        $leaves = [];
    }

    // save the current leaf value
    $leaves[$key] = $leaf;
}

var_dump($results);

这会产生:

array (size=5)
  0 => 
    array (size=3)
      'nome' => string 'Livello1' (length=8)
      'id' => int 47
      'level' => int 0
  1 => 
    array (size=3)
      'nome' => string 'Livello 2' (length=9)
      'id' => int 48
      'level' => int 1
  2 => 
    array (size=3)
      'nome' => string 'Livello 3' (length=9)
      'id' => int 50
      'level' => int 2
  3 => 
    array (size=3)
      'nome' => string 'Livello2bis' (length=11)
      'id' => int 49
      'level' => int 1
  4 => 
    array (size=3)
      'nome' => string 'Livello 3 Bis' (length=13)
      'id' => int 51
      'level' => int 2

基于以下数组结构:

$array = array(
    array(
        'nome' => 'Livello1',
        'id' => 47,
        'level' => 0,
        array(
            'nome' => 'Livello 2',
            'id' => 48,
            'level' => 1,
            array(
                'nome' => 'Livello 3',
                'id' => 50,
                'level' => 2,
            ),
        ),
        array(
            'nome' => 'Livello2bis',
            'id' => 49,
            'level' => 1,
            array(
                'nome' => 'Livello 3 Bis',
                'id' => 51,
                'level' => 2,
            ),
        ),
    ),
    array(
        'nome' => 'Livello 1 bis',
        'id' => 52,
        'level' => 0,
    ),
);

希望这会有所帮助:)