将不同数组中的值合并为一个具有相同键的值 [英] Merge values from different arrays to one with the same key
本文介绍了将不同数组中的值合并为一个具有相同键的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个数组:
Array
(
[0] => 5
[1] => 4
)
Array
(
[0] => BMW
[1] => Ferrari
)
我希望得到那个结果.合并具有相同键的值
And I would like to have that result. Merge the values with the same key
Array
(
[0] => Array
(
[0] => 5
[1] => BMW
)
[1] => Array
(
[0] => 4
[1] => Ferrari
)
)
我怎么能这样做?是否有任何本机 PHP
函数可以做到这一点?我尝试了 array_merge_recursive
和 array_merge
但没有得到预期的结果
How could I do that? Is there any native PHP
function that does this? I tried array_merge_recursive
and array_merge
but did not get the expected result
推荐答案
至于@splash58 评论:
As to @splash58 comment:
您可以使用 array-map.举个例子:
You can use array-map. Here an example:
$array = [["5", "4"], ["BMW", "Ferrari"]];
$res = array_map(null, ...$array);
现在 res
将包含:
Array
(
[0] => Array
(
[0] => 5
[1] => BMW
)
[1] => Array
(
[0] => 4
[1] => Ferrari
)
)
如果数组中有 2 个不同的变量,您可以使用:
If the array in 2 different var you can use:
$res= array_map(null, ["5", "4"], ["BMW", "Ferrari"]);
这篇关于将不同数组中的值合并为一个具有相同键的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文