使用相同的键将不同数组中的值合并到一个数组中
问题描述:
我有两个数组:
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个不同的var,则可以使用:
If the array in 2 different var you can use:
$res= array_map(null, ["5", "4"], ["BMW", "Ferrari"]);