I have a standard array with key-value pairs - and I want to use the keys to transform it into a multi-dimensional array. The difficulty seems to be that I need to loop recu
You can use a reference while walking though the array hierarchy levels:
function array_flat_to_multidimensional($arr) {
$out = array();
foreach ($arr as $compoundKey => $val) {
$ref = &$out;
foreach (explode(".", $compoundKey) as $key) {
if (!array_key_exists($key, $ref)) $ref[$key] = array();
$ref = &$ref[$key];
}
$ref = $val;
}
unset($ref);
return $out;
}