Home > Software design >  How to extract each level in nested array to separated arrays with specific key-value pairs?
How to extract each level in nested array to separated arrays with specific key-value pairs?

Time:12-15

I've got a 4-level nested array, each level represent a hiereachy in an organization. Each level's key is the hierarchy value, and inside it there is the id of it in the array. The last level is only the value-id pair:

$data =  [
    "top_level_data1" => [ 
            'top_level_id' => 0,           
            'sub_level_1' => [ 
                    'sub_level_1_id' => 0,                      
                    'sub_level_2_data1' => [
                            'sub_level_2_id' => 0,
                            'sub_level_2_data' => [
                                      0 => "some_val"
                                      1 => "some_other_val"
                                 ]
                        ]
              ]
        ],
    "top_level_data2" => [  
            'level_1_id' => 1,            
            'sub_level_1_other' => [      
                    'sub_level_1_id' => 1,              
                    'sub_level_2_data2' => [
                            'sub_level_2_id' => 1,
                            'sub_level_3_data1' => [
                                      2 => "another_val"
                                      3 => "bar"
                                      4 => "foo"
                                 ]
                        ],
                    'sub_level_2_data3' => [
                            'sub_level_2_id' => 2,
                            'sub_level_3_data2' => [
                                      5 => "foobar"
                                      6 => "hello"
                                      7 => "goodbye"
                                 ]
                        ]
              ]
        ] 
];

I want to extract it to separate arrays that would contain the hierarchy value-id pairs.

Expected output from the above example (without the ids of the above level):

$top_level = [
     0 => "top_level_data1",
     1 => "top_level_data2"
]

$sub_level_1 = [
    0 => "sub_level_1",
    1 => "sub_level_1_other"
]

$sub_level_2 = [
    0 => "sub_level_2_data1",
    1 => "sub_level_2_data2",
    2 => "sub_level_2_data3"
]

$sub_level_3 = [
    0 => "some_val"
    1 => "some_other_val"
    2 => "another_val"
    3 => "bar"
    4 => "foo"
    5 => "foobar"
    6 => "hello"
    7 => "goodbye"
]

I've tried using the RecursiveIterator but that does not work (At first I wanted to use it just to check if it iterates correctly, even before assignting the key-values as I wanted, but it just doesn't do it as I wanted):

$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
foreach($it as $key => $val) {
    $level[$it->getDepth()][] = $val;
}

CodePudding user response:

This can do it:

$result = [];
$recursive = null;
$recursive = function($args,$level=0) use (&$recursive,&$result){
    foreach($args as $k=>$v){
        if(is_array($v)){
            $result[$level][]=$k;
            $recursive($v,$level 1);
        } else if(is_string($v)){
            $result[$level][]=$v;
        }
    }
};
$recursive($data);
var_export($result);

But your expected output isn't ok. Because i think sub_level_2_data must be sub_level_3_data (in the $data array above). And the hole sub_level_3*level is missing in your expected output.

With this code you get an result array instead of separate filled variables.

Will not explain it in detail, but try to understand whats happening here and you can learn some new php stuff.

  •  Tags:  
  • php
  • Related