I need to create array which will look like this:
Array (
1 => Array(
2 => Array (
3 => Array ()
)
)
)
this array should be generated from array which looks like this:
Array (
1,
2,
3
)
I am not really sure how to better describe this in words, so thats why I show you this example. 3 values is only for purpose of this example, in real script number of values is vary. Thanks for any advice.
Solved, thanks for marking as duplicated. In case somebody will have same question as me, here is solution.
function setArrayValue(&$array, $path, $value) {
$temp = &$array;
foreach($path as $key)
$temp =& $temp[$key];
$temp = $value;
}
$keys = Array(
10,
9,
8,
7,
6,
5,
4,
3,
2,
1
);
$keys = array_reverse($keys);
$array = Array();
$path = Array();
foreach ($keys AS $i => $key) {
$path[] = $key;
if (array_key_exists(++$i, $keys))
$value = Array(
$keys[$i] => Array(),
);
else
$value = Array();
if ($i == 0) {
$array[$key] = $value;
} else {
setArrayValue($array, $path, $value);
}
}