PHP,合并在多维数组的键 [英] PHP, Merge keys in multidimensional array
问题描述
如果我有一个数组,它看起来是这样的:
If I have an array which looks something like this:
Array
(
[0] => Array
(
[DATA] => Array
(
VALUE1 = 1
VALUE2 = 2
)
)
[1] => Array
(
[DATA] => Array
(
VALUE3 = 3
VALUE4 = 4
)
)
)
和希望把它变成这样:
Array
(
[0] => Array
(
[DATA] => Array
(
VALUE1 = 1
VALUE2 = 2
VALUE3 = 3
VALUE4 = 4
)
)
)
我基本上要合并所有相同的按键,在同一水平。
什么是实现这一目标的最佳途径?
请问array_merge功能有什么用处?
I basically want to merge all the identical keys which are at the same level. What would be the best route to accomplish this? Could the array_merge functions be of any use?
我希望这是任何形式的意识,在此先感谢所有帮助我能。
I Hope this makes any sort of sense and thanks in advance for any help i can get.
推荐答案
您可以使用<一个href=\"http://php.net/manual/en/function.array-merge-recursive.php\"><$c$c>array_merge_recursive$c$c>合并所有项目的原始数组在了一起。而且,由于该函数采用可变数量的参数,使得它笨拙的时候这个数字是在编译时未知的,你可以使用<一个href=\"http://php.net/manual/en/function.call-user-func-array.php\"><$c$c>call_user_func_array$c$c>为了更加方便:
You can use array_merge_recursive
to merge all the items in your original array together. And since that function takes a variable number of arguments, making it unwieldy when this number is unknown at compile time, you can use call_user_func_array
for extra convenience:
$result = call_user_func_array('array_merge_recursive', $array);
结果将具有顶层的您的输入修剪掉(逻辑,因为你正在合并多个项目到一个),但将保留所有剩余的结构。
The result will have the "top level" of your input pruned off (logical, since you are merging multiple items into one) but will keep all of the remaining structure.
See it in action.
这篇关于PHP,合并在多维数组的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!