function recursive_array_convert ($input, &$result = array()) { $thisLevel = array_shift($input); if (count($input)) { if (!isset($result[$thisLevel]) || !is_array($result[$thisLevel])) { $result[$thisLevel] = array(); } recursive_array_convert($input, $result[$thisLevel]); } else { $result[$thisLevel] = NULL; } return $result; }
This function should give you more flexibility - you can just pass the input array to the first argument and catch the result in the return, or you can pass the existing variable in the second argument so that it fills the result, This means that you can achieve what you want in your example:
$result = recursive_array_convert(array('one', 'two', 'three', 'four'));
... or...
recursive_array_convert(array('one', 'two', 'three', 'four'), $result);
At first glance it may seem insignificant in this option, but pay attention to the following:
$result = array(); recursive_array_convert(array('one', 'two', 'three', 'four'), $result); recursive_array_convert(array('five', 'six', 'seven', 'eight'), $result); print_r($result);
As you can see, this function can be used to create whole chains, as you like in the same variable.
source share