1

I have a nested array containing numbers on multiple levels. The array key “number” will be holding an integer corresponding to that level and the key 'sub_level' will be holding an array with the same structure. Each level may or may not have a sub_level.

Sample Input

[
    0 => [
            'number' => 72,
            'sub_level' => [
                    'number' => 53,
                    'sub_level' => [
                            'number' => 49
                        ]
              ]
        ],
    1 => [
            'number' => 92,
            'sub_level' => [
                    'number' => 74
                ]
        ],
    2 => [
            'number' => 7
        ]
];

Sample o/p: 
Level 0: 72,92,7
Level 1: 53,74
Level 2: 49-->
1
  • 1
    You'd write a recursive function I guess. What have you tried? Commented Oct 14, 2020 at 11:00

3 Answers 3

2

This is easily solved with a recursive function, using array_column to get the number values at each level, and again to pass the sub_level to the recursive call:

function print_numbers($array, $level = 0) {
    if (empty($array)) return;
    echo "level $level: " . implode(',', array_column($array, 'number')) . "\n";
    print_numbers(array_column($array, 'sub_level'), $level+1);
}

print_numbers($input);

Output:

level 0: 72,92,7
level 1: 53,74
level 2: 49

If you want to extract all the numbers into a multi-dimensional array indexed by the level, you can modify the above function to return the values rather than echo them:

function save_numbers($array) {
    if (empty($array)) return array();
    return array_merge(array(array_column($array, 'number')), save_numbers(array_column($array, 'sub_level')));;
}

$out = save_numbers($input);
print_r($out);

Output:

Array
(
    [0] => Array
        (
            [0] => 72
            [1] => 92
            [2] => 7
        )
    [1] => Array
        (
            [0] => 53
            [1] => 74
        )
    [2] => Array
        (
            [0] => 49
        )
)

Demo on 3v4l.org

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

Comments

1

To solve this type of problem recursive is great.I have pushed the same levels in the $higher variable and the lower levels in the $lower variable. Then called the $lower var recursively.

function print_levels($array, $level = 0 ) {
    foreach ($array as $key1 => $value1) {
        foreach ($value1 as $key2 => $value2 ) {
            if( is_array( $value2 ) ){
                $lower [] = $value2;
            }else{
                $higher [] = $value2;
            }
        }
    }
    echo 'level '.$level. ': '.  implode( ',', $higher ) . '<br>';
    if( ! isset($lower) )
        return;
    else
        print_levels($lower, ++$level);
}

Comments

0

A solution with iterators that deliver the result as a multi-dimensional array. The implode function can be used to generate a list of the form 72,92,7.

$level = [];
$it =  new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach($it as $key => $element) {
  if($key == 'number'){
    $level[$it->getDepth()][] = $element;
  }
}

Result $level:

array (
  1 => 
  array (
    0 => 72,
    1 => 92,
    2 => 7,
  ),
  2 => 
  array (
    0 => 53,
    1 => 74,
  ),
  3 => 
  array (
    0 => 49,
  ),
) 

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.