I have xml that has been converted into array. The response array seems didn't create array[0] for one result, but create array[0], [1], [2] for many results. So I want to create array[0] for each one result response in order to standardize for my iteration. The initial response array as follow :
Array - One Detail
(
[BookedDetail] => Array
(
[BookedDetailID] => 192
[Customer] => Array
(
[CustomerID] => 110
[LanguageID] => 2
[Address] => Array
(
[StreetName] => Northway 23
[PostalCode] => 29843
[Region] => NSW
)
)
[Currency] => Array
(
[CurrencyID] => 19
[CurrencyName] => Yen
[CurrencyShortName] => JPY
)
[Status] => 1
[CreateBy] => 15
)
)
Array - Many Details
(
[BookedDetail] => Array
(
[0] => Array
(
[BookedDetailID] => 192
[Customer] => Array
(
[CustomerID] => 110
[LanguageID] => 2
[CustomerType] => 1
[Address] => Array
(
[StreetName] => Northway 23
[PostalCode] => 29843
[Region] => NSW
)
)
[Currency] => Array
(
[CurrencyID] => 19
[CurrencyName] => Yen
[CurrencyShortName] => JPY
)
[Status] => 3
[CreateBy] => 16
)
[1] => Array
(
[BookedDetailID] => 193
[Customer] => Array
(
[CustomerID] => 113
[LanguageID] => 2
[CustomerType] => 1
[Address] => Array
(
[StreetName] => Southway 23
[PostalCode] => 2852
[Region] => SSW
)
)
[Currency] => Array
(
[CurrencyID] => 23
[CurrencyName] => US Dollar
[CurrencyShortName] => USD
)
[Status] => 2
[CreateBy] => 17
)
)
)
My expected result :
(
[BookedDetail] => Array
(
[0] => Array
(
[BookedDetailID] => 192
[Customer] => Array
(
[0] => Array
(
[CustomerID] => 110
[LanguageID] => 2
[CustomerType] => 1
[Address] => Array
(
[0] => Array
(
[StreetName] => Northway 23
[PostalCode] => 29843
[Region] => NSW
)
)
)
)
[Currency] => Array
(
[0] = Array
(
[CurrencyID] => 19
[CurrencyName] => Yen
[CurrencyShortName] => JPY
)
)
[Status] => 3
[CreateBy] => 16
)
[1] => Array
(
[BookedDetailID] => 193
[Customer] => Array
(
[0] => Array
(
[CustomerID] => 113
[LanguageID] => 2
[CustomerType] => 1
[Address] => Array
(
[0] => Array
(
[StreetName] => Southway 23
[PostalCode] => 2852
[Region] => SSW
)
)
)
)
[Currency] => Array
(
[0] => Array
(
[CurrencyID] => 23
[CurrencyName] => US Dollar
[CurrencyShortName] => USD
)
)
[Status] => 2
[CreateBy] => 17
)
)
)
Each node can have one or multiple child nodes so I want to make [0] for each node and [0][1][2]etc for multiple child nodes. I tried some code and have problem in maintain the current array result in order to add in recursive functions. Do I need variable outside function to store my current array, or append it while call the recursive function. My current code :
$x1 = json_decode(json_encode($xmlobj), true);
$result = array();
function callarr ($arrin)
{
foreach ($arrin as $arrkey => $arrvalue)
{
foreach ($arrvalue as $subkey => $subvalue)
{
if (is_array($subvalue))
{
$arr1[$arrkey][$subkey] = $subvalue;
// recursive function here
}
else
{
$arr2[$arrkey][0][$subkey] = $subvalue;
}
}
$result = array_merge($arr1, $arr2);
}
return $result;
}
$x2 = callarr ($x1);
print_r ($x2);
Result of $x2 will create [0] node under [BookedDetail] only since I haven't call the recursive function yet. I need help to get my expected array result. Thanks a lot for your help.
It's difficult to know if this is exactly what you are after, but it may be easier for you to check it than me.
The function is now recursive, you can see where your comment has been removed and the assignment is being replaced by calling this function again with the $arrvalue
...
function callarr ($arrin)
{
$result = array();
foreach ($arrin as $arrkey => $arrvalue)
{
if (is_array($arrvalue))
{
$result[$arrkey][] = callarr($arrvalue);
}
else
{
$result[$arrkey] = $arrvalue;
}
}
return $result;
}