john_black
john_black

Reputation: 227

how to merge two arrays without duplication using a certain value in PHP?

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

Upvotes: 0

Views: 88

Answers (4)

Sovit Maharjan
Sovit Maharjan

Reputation: 175

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;

Upvotes: 0

The fourth bird
The fourth bird

Reputation: 163362

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

Upvotes: 2

maio290
maio290

Reputation: 6732

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

Upvotes: 2

Ram
Ram

Reputation: 84

try this to merge array

array_merge($array1,$array2)

Upvotes: 0

Related Questions