I have this array,
Array (
[0] => Array (
[P1] => Array (
[P2] => Array (
.
.
.
// n level
),
),
),
[1] => Array (
[P4] => Array (
[P5] => Array (
.
.
.
// n level
),
),
),
),
I want to convert it into this array
Array (
[0] => Array (
'id' => 0
[P1] => Array (
'id' => 'P1',
[P2] => Array (
'id' => 'P2'
.
.
.
// n level
),
),
),
[1] => Array (
'id' => '1'
[P4] => Array (
'id' => 'P4'
[P5] => Array (
'id' => 'P5'
.
.
.
n level
),
),
),
),
As you can see I took every key and map it to 'id' in its immediate body of an array.
This is effort I tried,
function recursive_function($arr)
{
$result = [];
if (is_array($arr)) {
foreach ($arr as $el) {
$result = array_merge($result, recursive_function($el));
}
} else {
$result[] = $input;
}
return $result;
}
I am new to recursive function and PHP stuff, so unable to think on how to and where to map and why.
Please help me get this output. Any help will be much appreciated.