0

I have two array, and i want to remove duplicate record from array2. i don't want to link_id 35 record in array 2 because link_id 35 record is present in array1 so it's not show in array2.

I tried with array_map and Unique methods but it's not working well because i think both the array doesn't have the same value.

$array1=

[0] => stdClass Object
        (
            [link_id] => 35
            [link_name] => Test Listerine cool mint packets 3 pack
            [alias] => aa
            [link_desc] => 
            [user_id] => 47
            [link_hits] => 103
            [link_votes] => 1
            [link_rating] => 5.000000
            [link_featured] => 0
            [link_published] => 1
            [link_approved] => 1
            [link_template] => 

        )

    [1] => stdClass Object
        (
            [link_id] => 373
            [link_name] => Test Subject Data Collection Fish Fresh Yellow Tail
            [alias] => ba
            [link_desc] =>
            [user_id] => 47
            [link_hits] => 198
            [link_votes] => 8
            [link_rating] => 2.875000
            [link_featured] => 0
            [link_published] => 1
            [link_approved] => 1
            [link_template] => 

        )



$array2 =

    [0] => stdClass Object
        (
            [link_id] => 35
            [link_name] => Test Listerine cool mint packets 3 pack
            [link_desc] => 
            [lat] => 0.000000
            [lng] => 0.000000
            [contactperson] => 
            [cat_name] => AA - Made in USA
            [link_votes] => 1
            [link_rating] => 5.000000
            [link_featured] => 0
            [value] => 30020864
        )

    [1] => stdClass Object
        (
            [link_id] => 541
            [link_name] => Test Subject Data Collection Fish Fresh Yellow Tail
            [link_desc] => 
            [lat] => 25.182573
            [lng] => -80.093079
            [country] => United States
            [postcode] => 33431
            [contactperson] => Captain Jack Certified Charters
            [cat_name] => BA - Product of USA
            [link_votes] => 8
            [link_rating] => 2.875000
            [link_featured] => 0
            [value] => NA
        )
3
  • Please include your actual snippet that shows up how far you got with your coding attempt. Commented Jan 17, 2019 at 6:00
  • 'code'$result = array_merge($array1,$array2); $allpage=array(); for($i=0; $i<count($result); $i++) { $allpage[] = $result[$i]->link_id; } $all_page1 =array_unique($allpage); $news =array(); $news1 =array(); foreach($result as $val) { if (in_array($val->link_id, $all_page1)) { $news[]=$val; } } echo "<pre>"; print_r($news1); exit; Commented Jan 17, 2019 at 6:10
  • In your question body as an edit please. Commented Jan 17, 2019 at 6:11

2 Answers 2

0

You can do that with array-filter. First extract all the ids from the first array and then filter the second array based of those ids.

$arr1 = array( (object) ["link_id"=> 35, "key" => "AAA"], (object) ["link_id"=> 373, "key" => "BBB"]);
$arr2 = array( (object) ["link_id"=> 35, "key" => "CCC"], (object) ["link_id"=> 341, "key" => "DDD"]);

$ids = array_column($arr1, "link_id");
$arr2 = array_filter($arr2, function ($e) use ($ids) {
    return !in_array($e->link_id, $ids); //keep him in arr2 only if NOT in ids of arr1
});

Updated more fast answer Consider big amount of data (as for @mickmackusa comment) use this:

$ids = [];
foreach($arr1 as $e)
    $ids[$e->link_id] = true;

$arr2 = array_filter($arr2, function ($e) use ($ids) {
    return !isset($ids[$e->link_id]);
});

First solution is in O(n^2) and the second is in O(n)

Sign up to request clarification or add additional context in comments.

Comments

0

This should do in php7.

Untested code:

var_export(array_diff_key(array_column($array2, null, 'link_id'), array_column($array1, null, 'link_id'));

Assign new 1st level keys to both arrays, then filter on those keys.

Checking against keys will be more efficient than making iterated calls of in_array.

2 Comments

Not working this code
I just updated it, I had the arrays in the wrong order. If you give me your input arrays as var_export I can create a demo. (I'm not at my computer, my phone slows me down)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.