PHP将一维数组转换为多维数组 [英] PHP convert one dimensional array into multidimensional
本文介绍了PHP将一维数组转换为多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组
$tmpArr = array('A', 'B', 'C');
我要处理此数组,并希望将新数组设为
I want to process this array and want new array as
$tmpArr[A][B][C] = C
即最后一个元素成为最终数组的值.
I.e last element becomes the value of final array.
有人可以建议解决方案吗?请帮忙.预先感谢
Can anyone suggest the solution? Please help. Thanks in advance
推荐答案
迭代键数组并使用引用表示链的末尾:
Iterate the array of keys and use a reference for the end of the chain:
$arr = array();
$ref = &$arr;
foreach ($tmpArr as $key) {
$ref[$key] = array();
$ref = &$ref[$key];
}
$ref = $key;
$tmpArr = $arr;
这篇关于PHP将一维数组转换为多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文