I have that single array and I need to convert in a multidimensional array without using array_merge
, array_replace_recurcive
etc, just an autonomous function:
$single = [
0 => 'one',
1 => 'two',
2 => 'tree',
3 => 'four',
4 => 'five'
];
And convert to look like this, with the last key as value:
$multidimentional = [
'one' => [
'two' => [
'tree' => [
'four' => 'five'
]
]
]
];
I have create a recursion function if this helps:
function array_replace_recursive($defaults, $replaces) {
if(is_null($replaces)) {
$replaces = [];
}
if(!is_array($defaults) || !is_array($replaces)) {
return $replaces;
}
foreach($defaults as $key => $value) {
if(!array_key_exists($key, $replaces) || is_null($replaces[$key])) {
$replaces[$key] = $value;
} else {
if(is_array($replaces[$key]) && is_array($value)) {
$replaces[$key] = array_replace_recursive($replaces[$key], $value);
}
}
}
return $replaces;
}