【问题标题】:How to merge 2 arrays in php? [duplicate]如何在php中合并2个数组? [复制]
【发布时间】:2017-05-17 20:12:03
【问题描述】:

对不起,这个基本问题,但我不知道怎么做。 我在 PHP 中有 2 个带有虚拟数据的数组

 Arr1[
   [0] => [
        [user_id] => 1,
        [first_name] => 'justin',
        [last_name] => 'bieber'
   ],

   [1] => [
        [user_id] => 2,
        [first_name] => 'terry',
        [last_name] => 'crews'
   ]
];

Arr2[
   [0] => [
        [user_id] => 1,
        [height] => 180,
        [weight] => 80
   ],

   [1] => [
        [user_id] => 2,
        [height] => 150,
        [weight] => 70
   ]
];

如何合并这些数组并得到结果数组?

ArrResult[
   [0] => [
        [user_id] => 1,
        [first_name] => 'justin',
        [last_name] => 'bieber'
        [height] => 180,
        [weight] => 80
   ],

   [1] => [
        [user_id] => 2,
        [first_name] => 'terry',
        [last_name] => 'crews'
        [height] => 150,
        [weight] => 70
   ]
];

你知道更好的方法吗?

【问题讨论】:

标签: php


【解决方案1】:

试试array_merge():

$merged = array();
foreach ($arr1 as $key => $value) {
    $merged[] = array_merge($arr1[$key], $arr2[$key]);
}

【讨论】:

    猜你喜欢
    • 2022-08-07
    • 1970-01-01
    • 2021-11-06
    • 2019-06-05
    • 2011-04-15
    • 2016-03-08
    • 2012-09-02
    • 2019-12-20
    • 1970-01-01
    相关资源
    最近更新 更多