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












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










share|improve this question


















  • 1





    Possible duplicate of How can you make a multidimensional array unique?

    – C2486
    Dec 31 '18 at 9: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 9:44











  • I would have use this answer: stackoverflow.com/a/4585286/6487675

    – dWinder
    Dec 31 '18 at 10:02
















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










share|improve this question


















  • 1





    Possible duplicate of How can you make a multidimensional array unique?

    – C2486
    Dec 31 '18 at 9: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 9:44











  • I would have use this answer: stackoverflow.com/a/4585286/6487675

    – dWinder
    Dec 31 '18 at 10:02














0












0








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










share|improve this question














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







php arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 31 '18 at 9:40









john_blackjohn_black

428




428








  • 1





    Possible duplicate of How can you make a multidimensional array unique?

    – C2486
    Dec 31 '18 at 9: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 9:44











  • I would have use this answer: stackoverflow.com/a/4585286/6487675

    – dWinder
    Dec 31 '18 at 10:02














  • 1





    Possible duplicate of How can you make a multidimensional array unique?

    – C2486
    Dec 31 '18 at 9: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 9:44











  • I would have use this answer: stackoverflow.com/a/4585286/6487675

    – dWinder
    Dec 31 '18 at 10:02








1




1





Possible duplicate of How can you make a multidimensional array unique?

– C2486
Dec 31 '18 at 9:43





Possible duplicate of How can you make a multidimensional array unique?

– C2486
Dec 31 '18 at 9: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 9:44





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 9:44













I would have use this answer: stackoverflow.com/a/4585286/6487675

– dWinder
Dec 31 '18 at 10:02





I would have use this answer: stackoverflow.com/a/4585286/6487675

– dWinder
Dec 31 '18 at 10:02












4 Answers
4






active

oldest

votes


















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






share|improve this answer


























  • You have id 1805 twice - why?

    – dWinder
    Dec 31 '18 at 10:00











  • @DavidWinder I have updated it, I switched the filtering.

    – The fourth bird
    Dec 31 '18 at 10:01











  • array_merge will do here won't it?

    – Progrock
    Dec 31 '18 at 10:48



















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





share|improve this answer































    0














    try this to merge array



    array_merge($array1,$array2)





    share|improve this answer
























    • You can merge, but this would not filter out ids that are already in the first array. 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;





    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985892%2fhow-to-merge-two-arrays-without-duplication-using-a-certain-value-in-php%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      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






      share|improve this answer


























      • You have id 1805 twice - why?

        – dWinder
        Dec 31 '18 at 10:00











      • @DavidWinder I have updated it, I switched the filtering.

        – The fourth bird
        Dec 31 '18 at 10:01











      • array_merge will do here won't it?

        – Progrock
        Dec 31 '18 at 10:48
















      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






      share|improve this answer


























      • You have id 1805 twice - why?

        – dWinder
        Dec 31 '18 at 10:00











      • @DavidWinder I have updated it, I switched the filtering.

        – The fourth bird
        Dec 31 '18 at 10:01











      • array_merge will do here won't it?

        – Progrock
        Dec 31 '18 at 10:48














      2












      2








      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






      share|improve this answer















      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







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Dec 31 '18 at 10:01

























      answered Dec 31 '18 at 9:50









      The fourth birdThe fourth bird

      22.6k81427




      22.6k81427













      • You have id 1805 twice - why?

        – dWinder
        Dec 31 '18 at 10:00











      • @DavidWinder I have updated it, I switched the filtering.

        – The fourth bird
        Dec 31 '18 at 10:01











      • array_merge will do here won't it?

        – Progrock
        Dec 31 '18 at 10:48



















      • You have id 1805 twice - why?

        – dWinder
        Dec 31 '18 at 10:00











      • @DavidWinder I have updated it, I switched the filtering.

        – The fourth bird
        Dec 31 '18 at 10:01











      • array_merge will do here won't it?

        – Progrock
        Dec 31 '18 at 10:48

















      You have id 1805 twice - why?

      – dWinder
      Dec 31 '18 at 10:00





      You have id 1805 twice - why?

      – dWinder
      Dec 31 '18 at 10:00













      @DavidWinder I have updated it, I switched the filtering.

      – The fourth bird
      Dec 31 '18 at 10:01





      @DavidWinder I have updated it, I switched the filtering.

      – The fourth bird
      Dec 31 '18 at 10:01













      array_merge will do here won't it?

      – Progrock
      Dec 31 '18 at 10:48





      array_merge will do here won't it?

      – Progrock
      Dec 31 '18 at 10:48













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





      share|improve this answer




























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





        share|improve this answer


























          2












          2








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





          share|improve this answer













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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 31 '18 at 9:50









          maio290maio290

          2,014414




          2,014414























              0














              try this to merge array



              array_merge($array1,$array2)





              share|improve this answer
























              • You can merge, but this would not filter out ids that are already in the first array. 3v4l.org/U46vj

                – Progrock
                Dec 31 '18 at 10:53


















              0














              try this to merge array



              array_merge($array1,$array2)





              share|improve this answer
























              • You can merge, but this would not filter out ids that are already in the first array. 3v4l.org/U46vj

                – Progrock
                Dec 31 '18 at 10:53
















              0












              0








              0







              try this to merge array



              array_merge($array1,$array2)





              share|improve this answer













              try this to merge array



              array_merge($array1,$array2)






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 31 '18 at 9:46









              RamRam

              397




              397













              • You can merge, but this would not filter out ids that are already in the first array. 3v4l.org/U46vj

                – Progrock
                Dec 31 '18 at 10:53





















              • You can merge, but this would not filter out ids that are already in the first array. 3v4l.org/U46vj

                – Progrock
                Dec 31 '18 at 10:53



















              You can merge, but this would not filter out ids that are already in the first array. 3v4l.org/U46vj

              – Progrock
              Dec 31 '18 at 10:53







              You can merge, but this would not filter out ids that are already in the first array. 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;





              share|improve this answer






























                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;





                share|improve this answer




























                  0












                  0








                  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;





                  share|improve this answer















                  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;






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 2 at 5:42

























                  answered Dec 31 '18 at 11:23









                  Sovit MaharjanSovit Maharjan

                  1269




                  1269






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985892%2fhow-to-merge-two-arrays-without-duplication-using-a-certain-value-in-php%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas