PHP - 基于值合并2个多维数组

时间:2017-07-25 13:31:30

标签: php arrays merge

目前,我有2个多维数组,我希望将它们组合成一个巨型数组,其中数组1中的值名称与数组2中的值名称相匹配。 阵列的外观如下......

Array1
(
    [0] => Array
        (
            [id] => 1
            [name] => test1
            [desc] => test_desc
            [quantity] => 3
        )

    [1] => Array
        (
            [id] => 2
            [name] => test2
            [desc] => test_desc
            [quantity] => 33
        )

)


Array2
(
    [0] => Array
        (
            [holder] => 'John'
            [name] => test1
            [desc] => test_desc
            [location] => ATL
        )

    [1] => Array
        (
            [holder] => 'Jackie'
            [name] => test3
            [desc] => test_desc
            [location] => SF
        )

)

我正在寻找合并array1中'name'列匹配array2中数组的数组,并将数组1中的列组合在一起。 2进入最终阵列。这应该看起来像......

FinalArray
(
    [0] => Array
        (
            [id] => 1
            [holder] => 'John'
            [name] => test1
            [desc] => test_desc
            [location] => ATL
            [quantity] => 3
        )

    [1] => Array
        (
            [holder] => 'Jackie'
            [name] => test3
            [desc] => test_desc
            [location] => SF
        )

    [2] => Array
        (
            [id] => 2
            [name] => test2
            [desc] => test_desc
            [quantity] => 33
        )

)

“test1”将2个数组中的不同列组合成“FinalArray”中的新数组。我已经尝试用array_merge和array_merge_recursive研究一些想法,但我不完全确定我是否朝着正确的方向前进。提前谢谢。

2 个答案:

答案 0 :(得分:1)

试试这个

$array1=[['id' => 1,'name' => 'test1','desc' => 'test_desc','quantity' => 3],
    ['id' => 2,'name' => 'test2','desc' => 'test_desc','quantity' => 33]];
$array2=[['holder' => 'John','name' => 'test1','desc' => 'test_desc','location' => 'ATL'],
    ['holder' => 'Jackie','name' => 'test3','desc' => 'test_desc','location' => 'SF']];
$final=[];
foreach ($array1 as $key1=>$data1){
    foreach ($array2 as $key2=>$data2){
        if($data1['name']==$data2['name']){
            $final[]=$data1+$data2;
            unset($array1[$key1]);
            unset($array2[$key2]);
        }
    }
}
if(!empty($array1)){
    foreach ($array1 as $value){
        $final[]=$value;
    }
}
if(!empty($array2)){
    foreach ($array2 as $value){
        $final[]=$value;
    }
}

它将输出为

enter image description here

答案 1 :(得分:1)

还有一个解决方案

function merge_by_name(array $arr1, array $arr2) {
    $result = [];

    foreach ($arr1 as $value) {

        $key = array_search($value['name'], array_column($arr2, 'name'));

        if($key !== false) {
            $result[] = array_merge($value, $arr2[$key]);
            unset($arr2[$key]);
        } else {
            $result[] = $value;
        }
    }
    $result = array_merge($result, $arr2);

    return $result;
}

测试

$arr1 = [
    [
        'id' => 1,
        'name' => 'test1',
        'desc' => 'test_desc',
        'quantity' => 3
    ],
    [
        'id' => 2,
        'name' => 'test2',
        'desc' => 'test_desc',
        'quantity' => 33
    ],
];

$arr2 = [
    [
        'holder' => 'John',
        'name' => 'test1',
        'desc' => 'test_desc',
        'location' => 'ATL'
    ],
    [
        'holder' => 'Jackie',
        'name' => 'test3',
        'desc' => 'test_desc',
        'location' => 'SF'
    ],
];

var_export(merge_by_name($arr1, $arr2));

结果

array (
  0 => 
  array (
    'id' => 1,
    'name' => 'test1',
    'desc' => 'test_desc',
    'quantity' => 3,
    'holder' => 'John',
    'location' => 'ATL',
  ),
  1 => 
  array (
    'id' => 2,
    'name' => 'test2',
    'desc' => 'test_desc',
    'quantity' => 33,
  ),
  2 => 
  array (
    'holder' => 'Jackie',
    'name' => 'test3',
    'desc' => 'test_desc',
    'location' => 'SF',
  ),
)