我是新手,将数组合并到多维数组有问题。我已经使用array_merge进行了测试,但工作正常。
我有这样的数组:
Array (
[0] => value 1
[1] => value 2
[2] => value 3
[3] => value 4
)
像这样的多维数组:
Array
(
[0] => Array
(
[name] => simple
[label] => Simple
[filter] => 1
[filterexcerpt] => 1
)
[1] => Array
(
[name] => compleks
[label] => Compleks
[filter] => 1
[filterexcerpt] => 1
)
[2] => Array
(
[name] => standard
[label] => Standard
[filter] =>
[filterexcerpt] =>
)
[3] => Array
(
[name] => abstract
[label] => Abstract
[filter] => 1
[filterexcerpt] => 1
)
)
如何将数组合并到多维数组,以及我想要的结果:
Array
(
[0] => Array
(
[name] => simple
[label] => Simple
[filter] => 1
[filterexcerpt] => 1
[value] => value 1
)
[1] => Array
(
[name] => compleks
[label] => Compleks
[filter] => 1
[filterexcerpt] => 1
[value] => value 2
)
[2] => Array
(
[name] => standard
[label] => Standard
[filter] =>
[filterexcerpt] =>
[value] => value 3
)
[3] => Array
(
[name] => abstract
[label] => Abstract
[filter] => 1
[filterexcerpt] => 1
[value] => value 4
)
)
请帮忙。
答案 0 :(得分:3)
循环小数组,并将其值放入更大的数组中,两个数组的索引都匹配。
foreach ($small_array as $key=>$value) {
$multidimensional_array[$key]['value'] = $value);
}
更改代码中显示的$small_array
和$multidimensional_array
。
上面的代码片段没有检查多维数组实际上是否有这样的索引,与小数组中的索引匹配。您可以在添加之前添加array_key_exist()
或isset()
支票,以使代码更加万无一失。