0

I need to iterate through multidimensional array. But I need to do it complex: iteration must go by levels - at first elements of 1 level, only after them elements of 2 level, after them elements of 3 level, etc.

I can write it myself if I want to, but I need to make sure I'm not reinventing the wheel. Is there any ready implementation of this in PHP?

UPDATE

What code should i provide? It's a theoretical issue. I can provide you an array.. :

Array
(
    [lev1_1] => Array
        (
            [ID] => 3547
            [children] => Array
                (
                    [lev2_1] => Array
                        (
                            [ID] => 3550
                        )

                    [lev2_2] => Array
                        (
                            [ID] => 3551
                        )
                )
        )
    [lev1_2] => Array
        (
            [ID] => 3547
            [children] => Array
                (
                    [lev2_3] => Array
                        (
                            [ID] => 3550
                            [children] => Array
                                (
                                    [lev3_1] => Array
                                        (
                                            [ID] => 3550
                                        )

                                    [lev3_2] => Array
                                        (
                                            [ID] => 3551
                                        )
                                )
                        )

                    [lev2_4] => Array
                        (
                            [ID] => 3551
                        )
                )
        )

I need iterate through lev1_x, then lev2_x, then lev3_x

7
  • 3
    Is there any code you gonna show to us ? Commented Mar 24, 2014 at 14:55
  • 1
    Please show us your code Commented Mar 24, 2014 at 14:56
  • To further emphasize. Please show us some code. Commented Mar 24, 2014 at 15:01
  • It is theoretical question. But i've posted update for you. Commented Mar 24, 2014 at 15:12
  • You need to provide the code you have tried so far to iterate through the array! we're not going to do your work for you! Unless you plan to pay that is. Commented Mar 24, 2014 at 15:12

1 Answer 1

1

Well, here's a dumb way to get the values by level. Please note that you'll need to tweak the code according to your use.

function array_values_by_level($array, $level = 0, &$values = null) {
    if ($values === null)
        $values = array();
    if (!isset($values[$level]))
        $values[$level] = array();
    foreach ($array as $v) {
        if (is_array($v))
            array_values_by_level($v, $level + 1, &$values);
        else
            $values[$level][] = $v;
    }

    return $values;
}

Test case:

$big_array = array(
    '1',
    '2',
    array(
        '2.1',
        '2.2',
        array(
            '2.2.1',
            '2.2.2',
            array(
                '2.2.2.1'
            )
        ),
        '2.3',
        array(
            '2.3.1',
            array(
                '2.3.1.1',
                '2.3.1.2'
            )
        )
    )
);

foreach (array_values_by_level($big_array) as $k => $v)
    printf("[%d] %s \n", $k, implode(', ', $v));

// [0] 1, 2
// [1] 2.1, 2.2, 2.3
// [2] 2.2.1, 2.2.2, 2.3.1
// [3] 2.2.2.1, 2.3.1.1, 2.3.1.2
Sign up to request clarification or add additional context in comments.

Comments

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.