Reputation: 23
I have php array like this:
$array = array('1', '2', '3', '5', '8', '11');
But I want to get this one:
$result['1']['2']['3']['5']['8']['11'];
Note: Number of elements in array $array are not constant.
Upvotes: 2
Views: 1567
Reputation: 522382
$result = array();
foreach (array_reverse($array) as $key) {
$result = array($key => $result);
}
// or, with PHP 5.4 array syntax and functional code:
$result = array_reduce(array_reverse($array), function (array $result, $key) {
return [$key => $result];
}, []);
Not sure if this is really what you imagine it will be though.
Upvotes: 3