I need to explode the key values of an array and build a new array which has the exploded result. Below code works with one sub-array and I think I am missing a for loop to take care of the array values iteration.
The solution should also process the 'finance' subarray data, to be exploded and visible in the new array.
I would have 9 sub-arrays in later stage, thus the reason of needing to explode the data and move result into new array.
My code
<?php
$array = [
'company_info' => [
'country_period_0' => 10,
'currency_period_0' => 20
],
'finance' => [
'values_period_0' => 30
]
];
$newArray = [];
for ($i=0; $i <= 1 ; $i++) {
$array_1 = $array['company_info'];
$arrayKeys = array_keys($array_1);
$arrayValues = array_values($array_1);
$keySplits = explode("_", $arrayKeys[$i]);
for ($i=0; $i <= 2 ; $i++) {
$newArray[] = $keySplits[$i];
}
$newArray[3] = $arrayValues[0];
}
print_r($newArray);
Result
Array(
[0] => country
[1] => period
[2] => 0
[3] => 10
)
Wanted result
['company_info]
Array(
[0] => country
[1] => period
[2] => 0
[3] => 10
)
Array(
[0] => currency
[1] => period
[2] => 0
[3] => 20
)
['finance']
Array(
[0] => values
[1] => period
[2] => 0
[3] => 30
)
$iin both outer and innerforloop. In the inner loop, it increases$iat a point it reached the max value of the outer one ($i <= 1). This won't fix your whole code, but this is a good start