很抱歉这么长的描述,但我已经为合并多个多维关联数组苦苦挣扎了一天多了。任何人都可以建议实现这一目标的优化方法。我想根据所需的结果创建 HTML 列表。请参阅当前列表的附加图像。但我想将孩子合并为共同的父母。阵列 1:Array( [cat_id] => 1 [parent_cat_id] => 0 [cat_name] => Collectables [children] => Array ( [cat_id] => 1335 [parent_cat_id] => 1 [cat_name] => Animal Collectables [children] => Array ( [cat_id] => 10811 [parent_cat_id] => 1335 [cat_name] => Bird Collectables [children] => Array ( [cat_id] => 37847 [parent_cat_id] => 10811 [cat_name] => Bluebird ) ) ))阵列 2:Array( [cat_id] => 1 [parent_cat_id] => 0 [cat_name] => Collectables [children] => Array ( [cat_id] => 1335 [parent_cat_id] => 1 [cat_name] => Animal Collectables [children] => Array ( [cat_id] => 10811 [parent_cat_id] => 1335 [cat_name] => Bird Collectables [children] => Array ( [cat_id] => 37848 [parent_cat_id] => 10811 [cat_name] => Hummingbird ) ) ))我希望结果保持原样,并在同一级别创建另一个子数组子数组,如下所示。Array( [cat_id] => 1 [parent_cat_id] => 0 [cat_name] => Collectables [children] => Array ( [cat_id] => 1335 [parent_cat_id] => 1 [cat_name] => Animal Collectables [children] => Array ( [cat_id] => 10811 [parent_cat_id] => 1335 [cat_name] => Bird Collectables [children] => Array ( [0] => Array( [cat_id] => 37847 [parent_cat_id] => 10811 [cat_name] => Bluebird ) [1] => Array( [cat_id] => 37848 [parent_cat_id] => 10811 [cat_name] => Hummingbird ) ) ) ))[1]:当前列表:https ://i.stack.imgur.com/p4F0X.png
1 回答
胡说叔叔
TA贡献1804条经验 获得超8个赞
array_merge_recursive应该可以帮助你。
一个例子是:
<?php
$arr1['foo']['bar'] = 'foobar';
$arr2['foo']['bar'] = 'barfoo';
print_r(array_merge_recursive($arr1, $arr2));
输出:
Array
(
[foo] => Array
(
[bar] => Array
(
[0] => foobar
[1] => barfoo
)
)
)
- 1 回答
- 0 关注
- 99 浏览
添加回答
举报
0/150
提交
取消