PHP - Merge 2 multidimensional array based on value

时间:2021-12-15 10:45:46

Currently, I have 2 multidimensional array and I'm looking to combine them into one giant array where the value's name in array 1 matches the value's name in array 2. The array's look as followed...

目前,我有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
        )

)

I'm looking to merge the arrays where the 'name' column in array1 matches in array2 and also combine the columns in array's 1 & 2 into the final array. This should look like...

我想要合并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
        )

)

Where the "test1" combines the different columns across the 2 arrays into a new array inside the "FinalArray". I've tried researching some ideas with array_merge and array_merge_recursive but I'm not entirely sure if I'm going in the correct direction. Thanks in advance.

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

2 个解决方案

#1


1  

Try like this

试试这样吧

$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;
    }
}

It will give output as

它将输出为

PHP - Merge 2 multidimensional array based on value

#2


1  

One more solution

还有一个解决方案

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;
}

Test

$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));

Result

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',
  ),
)

#1


1  

Try like this

试试这样吧

$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;
    }
}

It will give output as

它将输出为

PHP - Merge 2 multidimensional array based on value

#2


1  

One more solution

还有一个解决方案

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;
}

Test

$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));

Result

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',
  ),
)