PHP创建数组的3D阵列与3个值 [英] PHP create 3D Array from Array with 3 Values
本文介绍了PHP创建数组的3D阵列与3个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我再次:
以下数组;
$ myArray的=阵列('富','酒吧','HELLO');
我需要:
$ myArray的['富'] ['BAR'] ['HELLO'] = 0;
什么想法?
解决方案
函数洋葱($ A){
返回$一个?阵列(array_shift($ A)=>洋葱($ A)):0;
}$ myArray的=阵列('富','酒吧','HELLO');
的print_r(洋葱($ myArray的));
//编辑:其实<一个href=\"http://stackoverflow.com/questions/1722632/php-create-3d-array-from-array-with-3-values/1722778#1722778\">doc's解决方案是更好的,他/她的code,有点改进
$新= 0;
的foreach(array_reverse($ myArray的)为$ V)
$ =新阵列(V $ =&GT; $新);
again me:
following array;
$myArray = array('FOO', 'BAR', 'HELLO');
i need:
$myArray['FOO']['BAR']['HELLO'] = 0;
any ideas?
解决方案
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);
这篇关于PHP创建数组的3D阵列与3个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文