Home > other >  How to give names to arrays inside RecursiveArrayIterator?
How to give names to arrays inside RecursiveArrayIterator?

Time:01-23

Originally, I manually created nested foreach loops:

$first_array_counter = 0;
$second_array_counter = 0;
$third_array_counter= 0;
$fourth_array_counter = 0;

foreach ($nested_array_4_layers as $layer1_items) {
    $first_array['first_array_id'] = $first_array_counter;
    foreach ($layer1_items as $layer2_items) {
        $second_array['second_array_id'] = $second_array_counter;
        foreach ($layer2_items as $layer3_items) {
            $third_array['third_array_id'] = $third_array_counter; 
            foreach ($layer3_items as $layer3_items) {
                $fourth_array['fourth_array_id'] = $fourth_array_counter;
                $fourth_array_counter  ;
            }
            $third_array_counter  ;
        }
        $second_array_counter  
    }
    $first_array_counter  ;
}

But now I'm trying to change the code to be dynamic and with varying nesting levels. For example with 3 nesting levels and different names:

foreach ($nested_array_3_layers as $layer1_items) {
    $other_first_array['first_array_id'] = $first_array_counter;
    foreach ($layer1_items as $layer2_items) {
        $other_second_array['second_array_id'] = $second_array_counter;
        foreach ($layer2_items as $layer3_items) {
            $other_third_array['third_array_id'] = $third_array_counter;                 
            $third_array_counter  ;
        }
        $second_array_counter  
    }
    $first_array_counter  ;
}

For that I changed the code to use RecursiveIteratorIterator with RecursiveArrayIterator:

$iter = new RecursiveIteratorIterator(new RecursiveArrayIterator($nested_array_x_layers), RecursiveIteratorIterator::SELF_FIRST);

foreach ($iter as $k => $v) {
    if (!is_array($v)) {       
    }
}

But I still want to be able to control which names the arrays have because I need to later use them.

Would that be done using maybe a switch-case and given options for the array names?

But then, how can I control which nesting level belongs to which name?

CodePudding user response:

It is not recommended to use many loops at the same time, by defining keys, combining the arrays first will provide ease of use later.

Table 1

$array_1_data = Array (
[0] => Array ( [id] => 1 [name] => IT [slug] => it )
[1] => Array ( [id] => 2 [name] => Accounting [slug] => accounting )
)

Table 2

$array_2_data = Array (
[0] => Array ( [cid] => 3 [jid] => 24061 )
[1] => Array ( [cid] => 1 [jid] => 24062 )
)

Joining via key

$result = array_uintersect_uassoc($array_1_data, $array_2_data, function($a, $b){
return strcasecmp($a['id'], $b['cid']);
}, function($a, $b){
return (int) [$a, $b] == ['id', 'cid'];
});

conclusion

Array
(
[0] => Array
(
[id] => 1
[name] => IT
[slug] => it
)
)

CodePudding user response:

You can use the getDepth() method of the RecursiveIteratorIterator class to determine the current nesting level, and then use a switch-case statement or a series of if-else statements to assign different names to the arrays depending on the current nesting level.

Here is an example of how you can do this:

$iter = new RecursiveIteratorIterator(new RecursiveArrayIterator($nested_array_x_layers), RecursiveIteratorIterator::SELF_FIRST);
foreach ($iter as $k => $v) {
    if (!is_array($v)) {
        switch($iter->getDepth()) {
            case 0:
                $first_array[$k] = $v;
                break;
            case 1:
                $second_array[$k] = $v;
                break;
            case 2:
                $third_array[$k] = $v;
                break;
            case 3:
                $fourth_array[$k] = $v;
                break;
        }
    }
}

You can also use a while loop and check the depth of the iterator and then assign array name accordingly. It will give you more flexibility in terms of customizing the array names and handling different nesting levels.

$iter = new RecursiveIteratorIterator(new RecursiveArrayIterator($nested_array_x_layers));
while($iter->valid()) {
    if (!$iter->hasChildren()) {
        switch($iter->getDepth()) {
            case 0:
                $first_array[$iter->key()] = $iter->current();
                break;
            case 1:
                $second_array[$iter->key()] = $iter->current();
                break;
            case 2:
                $third_array[$iter->key()] = $iter->current();
                break;
            case 3:
                $fourth_array[$iter->key()] = $iter->current();
                break;
        }
    }
    $iter->next();
}

This way you can control which names the arrays have and also which nesting level belongs to which name.

  •  Tags:  
  • php
  • Related