1

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
             )

    ......
    )

?

2
  • The first array it's the results of a recursive function, i need to foreach it into a view file,so it' simpler for me foreach a single level array...i dont know the logic behind it.. Commented Nov 30, 2013 at 16:49
  • can you tell us from which source you are getting the multidimensional array? Commented Nov 30, 2013 at 17:09

1 Answer 1

4

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 :)

Sign up to request clarification or add additional context in comments.

4 Comments

Oops. I initially forgot the $key for each leaf - answer updated.
@Darragh thanks for your help! it works but i lost the last array child 'Livello 1 bis', you know why? but i understand the logic so thanks so much
@StefanoSchivari - ah, you're correct, there must be a flaw in my logic. looking into it.
Ah... after the loop. When foreach stops iterating it has populated the $leaves array but it does not add it to $results. So, you can simply add $results[] = $leaves; after the loop. Not very elegant perhaps but that will sort it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.