简体   繁体   中英

PHP: Split multidimensional array to arrays?

What is the best way to convert this 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
        )

)

to this format:

    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
             )

    ......
    )

?

You could use a RecursiveIteratorIterator() (what a mouthful) to iterate over the leaves of your nested array, create a new array for each level using RecursiveIteratorIterator::getDepth() and add it to a result set. For example:

<?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);

This yields:

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

Based on the following array structure:

$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,
    ),
);

Hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM