假设我有此数组:转换一个维阵列成多维数组
Array
(
[0] => Array
(
[name] => ELECTRONICS
[depth] => 0
)
[1] => Array
(
[name] => TELEVISIONS
[depth] => 1
)
[2] => Array
(
[name] => TUBE
[depth] => 2
)
[3] => Array
(
[name] => LCD
[depth] => 2
)
[4] => Array
(
[name] => PLASMA
[depth] => 2
)
[5] => Array
(
[name] => PORTABLE ELECTRONICS
[depth] => 1
)
)
我希望它被转换成一个多维阵列,使得以比以前的元件更高的深度立即元件将使用关键字“children”进入前一个元素。像这样:
Array
(
[0] => Array
(
[name] => ELECTRONICS
[depth] => 0
[children] => Array
(
[0] => Array
(
[name] => TELEVISIONS
[depth] => 1
[children] => Array
(
[0] => Array
(
[name] => TUBE
[depth] => 2
)
[1] => Array
(
[name] => LCD
[depth] => 2
)
[2] => Array
(
[name] => PLASMA
[depth] => 2
)
)
)
[1] => Array
(
[name] => PORTABLE ELECTRONICS
[depth] => 1
)
)
)
)
非常感谢您的帮助。谢谢;)
目前我在我束手无策尝试使用一对夫妇如果在一个for循环但遗憾的是无济于事的条件... – VeeBee 2013-02-19 15:58:39
有这里的之间的代码没有逻辑连接来-了一些array_push方法父母和孩子。您在初始输入中唯一知道的是深度级别。人们可能会根据深度创建一棵树,但看起来像嵌套像条款一样。 TUBE,LCD和PLASMA是电视的孩子,但PORTABLE ELECTRONICS没有孩子。代码如何知道这种关系? – tmsimont 2013-02-19 15:59:57
哎呀,对不起,我错过了这个:“我希望它被转换成一个多维数组,使深度高于前一个元素的直接元素将进入前一个元素的内部,使用键”children“” – tmsimont 2013-02-19 16:00:52