I’m using the following function to flatten a multidimensional array:
function flatten($array, $prefix = '') {
$result = array();
foreach($array as $key=>$value) {
if(is_array($value)) {
$result = $result + flatten($value, $prefix . $key . '.');
}
else {
$result[$prefix . $key] = $value;
}
}
return $result;
}
I’d like to create a matching function, unflatten, that will reverse the process(e.g create a child array if the key has a . in it). Any ideas?
Use
parse_strto get this done.just try below.Outputs: