Combine two 2d arrays and have duplicate rows removed
Asked Answered
P

3

1

How do I create a new multidimensional array by merging two arrays and removing any duplicate rows.

First array:

[
    0 => ["id" => "0001", "name" => "sample name 1"],
    1 => ["id" => "0002", "name" => "sample name 2"],
    3 => ["id" => "0003", "name" => "sample name 3"]
]

Second Array:

[
    0 => ["id" => "0002", "name" => "sample name 2"],
    1 => ["id" => "11323", "name" => "blah blah"]
]

The desired result is:

[
    ["id" => "0001", "name" => "sample name 1"],
    ["id" => "0002", "name" => "sample name 2"],
    ["id" => "0003", "name" => "sample name 3"]
    ["id" => "11323", "name" => "blah blah"]
]
Promise answered 21/9, 2012 at 21:20 Comment(0)
I
1

You can do this

$array1 = Array(
        0 => Array("id" => "0001","name" => "sample name 1"),
        1 => Array("id" => "0002","name" => "sample name 2"),
        3 => Array("id" => "0003","name" => "sample name 3"));

$array2 = Array(
        0 => Array("id" => "0002","name" => "sample name 2"),
        1 => Array("id" => "11323","name" => "blah blah"));

$output = array_map("unserialize", array_unique(array_map("serialize", array_merge($array1,$array2))));

var_dump($output);

Output

array
  0 => 
    array
      'id' => string '0001' (length=4)
      'name' => string 'sample name 1' (length=13)
  1 => 
    array
      'id' => string '0002' (length=4)
      'name' => string 'sample name 2' (length=13)
  2 => 
    array
      'id' => string '0003' (length=4)
      'name' => string 'sample name 3' (length=13)
  4 => 
    array
      'id' => string '11323' (length=5)
      'name' => string 'blah blah' (length=9)
Interknit answered 21/9, 2012 at 21:38 Comment(0)
J
1

You don't need to reduce your rows of data to be serialized strings to check for uniqueness. Just call array_udiff() on the second array to remove first array rows from it, then merge that result with the first array. array_udiff() is optimized under the hood because it uses a sorting algorithm to compare data -- in this way, it is more performant than making brute-force iterations of in_array().

Code: (Demo)

$array1 = [
    0 => ["id" => "0001", "name" => "sample name 1"],
    1 => ["id" => "0002", "name" => "sample name 2"],
    3 => ["id" => "0003", "name" => "sample name 3"]
];

$array2 = [
    0 => ["id" => "0002", "name" => "sample name 2"],
    1 => ["id" => "11323", "name" => "blah blah"]
];

var_export(
    array_merge($array1, array_udiff($array2, $array1, fn($a, $b) => $a <=> $b))
);

Because whole rows are being compared, you can also merge the arrays and use the SORT_REGULAR flag with array_unique(). (Demo)

var_export(
    array_unique(array_merge($array1, $array2), SORT_REGULAR)
);
Japonica answered 1/9, 2022 at 21:35 Comment(0)
A
0

You can serialize each subarray, use array_unique, then unserialize back:

$arr = array_merge($arr1,$arr2);
foreach($arr as &$a) {
    $a = serialize($a);
}
$arr = array_values(array_unique($arr));
foreach($arr as &$a) {
    $a = unserialize($a);
}
Astrodome answered 21/9, 2012 at 21:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.