I have a condition to increment the array keys in such a way that each array key is to get increment. Only this part should get updated with the incremented number “LV001_LV002” to “LV099_LV100”.(But not only up to 100 it get increment above 100 also).
i have developed the below structure of array:
`array:2 [ "Level1" => array:3 [ "LV001_LV002" => array:5 [ "L1" => "string" "L2" => "string" "L3" => "string" "L4" => "string" "L5" => "string" ] "LV003_LV004" => array:5 [ "L1" => "string" "L2" => "string" "L3" => "string" "L4" => "string" "L5" => "string" ] "LV005_LV006" => array:5 [ "L1" => "string" "L2" => "string" "L3" => "string" "L4" => "string" "L5" => "string" ] ] "Level2" => array:3 [ "LV001_LV002" => array:5 [ "L1" => "string" "L2" => "string" "L3" => "string" "L4" => "string" "L5" => "string" ] "LV003_LV004" => array:5 [ "L1" => "string" "L2" => "string" "L3" => "string" "L4" => "string" "L5" => "string" ] "LV005_LV006" => array:5 [ "L1" => "string" "L2" => "string" "L3" => "string" "L4" => "string" "L5" => "string" ] ] ]`
In above many level1 to (n)levels… will be coming, So how i can do this. we have to change only this part as the serial number “LV001_LV002” up to “LVxxx_LVxxx”.
I want the result should be like this:
`[ "LV001_LV002" => array:5 [ "L1" => "string" "L2" => "string" "L3" => "string" "L4" => "string" "L5" => "string" ] "LV003_LV004" => array:5 [ "L1" => "string" "L2" => "string" "L3" => "string" "L4" => "string" "L5" => "string" ] "LV005_LV006" => array:5 [ "L1" => "string" "L2" => "string" "L3" => "string" "L4" => "string" "L5" => "string" ] "LV007_LV008" => array:5 [ "L1" => "string" "L2" => "string" "L3" => "string" "L4" => "string" "L5" => "string" ] "LV009_LV010" => array:5 [ "L1" => "string" "L2" => "string" "L3" => "string" "L4" => "string" "L5" => "string" ] "LV011_LV012" => array:5 [ "L1" => "string" "L2" => "string" "L3" => "string" "L4" => "string" "L5" => "string" ] ]`
how can I do that?
Advertisement
Answer
You can do it like this,
<?php $mainArray = [ "Level1" => [ "LV001_LV002" => [ "L1" => "string", "L2" => "string", "L3" => "string", "L4" => "string", "L5" => "string", ], "LV003_LV004" => [ "L1" => "string", "L2" => "string", "L3" => "string", "L4" => "string", "L5" => "string", ], "LV005_LV006" => [ "L1" => "string", "L2" => "string", "L3" => "string", "L4" => "string", "L5" => "string", ] ], "Level2" => [ "LV001_LV002" => [ "L1" => "string", "L2" => "string", "L3" => "string", "L4" => "string", "L5" => "string", ], "LV003_LV004" => [ "L1" => "string", "L2" => "string", "L3" => "string", "L4" => "string", "L5" => "string", ], "LV005_LV006" => [ "L1" => "string", "L2" => "string", "L3" => "string", "L4" => "string", "L5" => "string", ] ] ]; $newArray = []; $indexCount = 1;// Array index foreach ($mainArray as $array) { foreach ($array as $key=>$value) { // Generate index $index = "LV" . str_pad($indexCount++, 3, '0', STR_PAD_LEFT) . "_LV" . str_pad($indexCount++, 3, '0', STR_PAD_LEFT); $newArray[$index] = $value;// Store with generated index } } print_r($newArray);// To see the output
Output:
Array ( [LV001_LV002] => Array ( [L1] => string [L2] => string [L3] => string [L4] => string [L5] => string ) [LV003_LV004] => Array ( [L1] => string [L2] => string [L3] => string [L4] => string [L5] => string ) [LV005_LV006] => Array ( [L1] => string [L2] => string [L3] => string [L4] => string [L5] => string ) [LV007_LV008] => Array ( [L1] => string [L2] => string [L3] => string [L4] => string [L5] => string ) [LV009_LV010] => Array ( [L1] => string [L2] => string [L3] => string [L4] => string [L5] => string ) [LV011_LV012] => Array ( [L1] => string [L2] => string [L3] => string [L4] => string [L5] => string ) )