0

in PHP, I have these two arrays, first one is "$ar1", the second is "$ar2":

Array
(
    [0] => stdClass Object
        (
            [id] => 1505
            [category] => blue
        )

    [1] => stdClass Object
        (
            [id] => 1805
            [category] => red
        )

)
Array
(
    [1777] => stdClass Object
        (
            [id] => 1505
            [category] => yellow
        )

    [1877] => stdClass Object
        (
            [id] => 1507
            [category] => blue
        )

)

I want to add $ar1 to $ar2 but only if the "id" is not already in $ar2. for example: the one with id "1505" should not be added to $ar2. here is the code I used:

$ar3 = array_merge_recursive($ar1, $ar2);

john_black
  • 167
  • 3
  • 14
  • 1
    Possible duplicate of [How can you make a multidimensional array unique?](https://stackoverflow.com/questions/4585208/how-can-you-make-a-multidimensional-array-unique) – Niklesh Raut Dec 31 '18 at 09:43
  • also: I don't want to use foreach for this to compare the "id" part because this is just a sample and the arrays size are actually huge so i'm concerned about performance – john_black Dec 31 '18 at 09:44
  • I would have use this answer: https://stackoverflow.com/a/4585286/6487675 – dWinder Dec 31 '18 at 10:02

4 Answers4

2

Probably not the most php-ish code (I guess there is some array function which can do this better), but it's working:

$array1 = array(
    new Dummy(1505,"blue"),
    new Dummy(1805,"red")
);

$array2 = array(
    new Dummy(1505,"yellow"),
    new Dummy(1507,"blue")
);

$array3 = [];

foreach($array1 as $value)
{
    $array3[$value->id] = $value;
}

foreach($array2 as $value)
{
    if(!isset($array3[$value->id]))
    {
        $array3[$value->id] = $value;
    }
}

var_dump($array3);

I just made a Dummy class, since I wanted to be as near to your data as I could. This uses the key of the map (arrays are maps in PHP) as primary key. First, you iterate over your always setting array, then over the one which only gets applied to the final array if the id wasn't set yet -> done. This prints:

array(3) { 
[1505]=> object(Dummy)#1 (2) { ["id"]=> int(1505) ["category"]=> string(4) "blue"}
[1805]=> object(Dummy)#2 (2) { ["id"]=> int(1805) ["category"]=> string(3) "red" } 
[1507]=> object(Dummy)#4 (2) { ["id"]=> int(1507) ["category"]=> string(4) "blue" } 
}
maio290
  • 6,440
  • 1
  • 21
  • 38
2

One possibility might by to first extract the ids from $ar2 using array_column and use array_filter:

$ar2 = array( '1777' => (object) array('id' => '1505', 'category' => 'yellow'), '1877' => (object) array('id' => '1507', 'category' => 'blue'), );
$ids = array_column($ar1, 'id');
$ar3 = array_merge_recursive($ar1, array_filter($ar2, function($x) use ($ids) {
    return !in_array($x->id, $ids);
}));

print_r($ar3);

Result:

Array
(
    [0] => stdClass Object
        (
            [id] => 1505
            [category] => blue
        )

    [1] => stdClass Object
        (
            [id] => 1805
            [category] => red
        )

    [2] => stdClass Object
        (
            [id] => 1507
            [category] => blue
        )

)

Php demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

try this to merge array

array_merge($array1,$array2)
Ram
  • 84
  • 7
  • You can merge, but this would not filter out ids that are already in the first array. https://3v4l.org/U46vj – Progrock Dec 31 '18 at 10:53
0

try this, function is from this link: http://php.net/manual/en/function.array-unique.php

$array1 = array(
                    array('id'=>'1505', 'category'=>'blue'),
                    array('id'=>'1805', 'category'=>'red')
                );

      $array2 = array(
                    array('id'=>'1505', 'category'=>'yellow'),
                    array('id'=>'1507', 'category'=>'blue')
                );
      $array3 = array_merge($array1, $array2);

      function unique_multidim_array($array, $key) { 
          $temp_array = array(); 
          $i = 0; 
          $key_array = array(); 

          foreach($array as $val) { 
              if (!in_array($val[$key], $key_array)) { 
                  $key_array[$i] = $val[$key]; 
                  $temp_array[$i] = $val; 
              } 
              $i++; 
          } 
          return $temp_array; 
      } 
      $details = unique_multidim_array($array3,'id');
      echo "<pre>";
      print_r($details);
      die;