使用相同的键将不同数组中的值合并到一个数组中 [英] Merge values from different arrays to one with the same key

查看:135
本文介绍了使用相同的键将不同数组中的值合并到一个数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组:

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_recursivearray_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"]);

这篇关于使用相同的键将不同数组中的值合并到一个数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆