Hi,
again me:
following array;
$myArray = array('FOO', 'BAR', 'HELLO');
i need:
$myArray['FOO']['BAR']['HELLO'] = 0;
any ideas?
Hi,
again me:
following array;
$myArray = array('FOO', 'BAR', 'HELLO');
i need:
$myArray['FOO']['BAR']['HELLO'] = 0;
any ideas?
$newArray = array($oldArray[0] => array($oldArray[1] => array($oldArray[2] => 0)));
you could do it with recursion:
function arrToKeys(&$arr,$initialCount){
if($initialCount == 0 ){
return $arr;
}
else{
$newKey = $arr[0];
unset($arr[0]);
$arr[$newKey] = $arr;
$initialCount--;
return arrToKeys($arr,$initialCount);
}
}
//then call it like this
$newArr = arrToKeys($myArray,count($myArray));
function onion($a) {
return $a ? array(array_shift($a) => onion($a)) : 0;
}
$myArray = array('FOO', 'BAR', 'HELLO');
print_r(onion($myArray));
//edit: actually doc's solution is better, his/her code, a bit improved
$new = 0;
foreach (array_reverse($myArray) as $v)
$new = array($v => $new);
$newarr[array_pop($myarr)] = 0;
foreach (array_reverse($myarr) as $v)
$newarr[$v] = $newarr;