iam
iam

Reputation: 1013

Array Merge and remove duplicate

I have two array from different queries:

Array
(
    [41] => 1
    [42] => 2
) 

Array
(
    [42] => 30215
    [43] => 15478
)

Now I want to have an array of all the items of the second array that are not duplicates of the first one.

Array
(
    [43] => 15478
)

key => 42 is deleted out of the array, because it exists in the first array.

Upvotes: 0

Views: 95

Answers (2)

Rizier123
Rizier123

Reputation: 59691

This should work for you:

Just use array_diff_key() to get the difference by the key, like this:

<?php

    $arr1 = [41 => 1, 42 => 2];
    $arr2 = [42 => 30215, 43 => 15478];

    print_r(array_diff_key($arr2, $arr1));

?>

output:

Array( [43] => 15478 )

Upvotes: 2

Manish Prajapati
Manish Prajapati

Reputation: 1641

try to do with $desired_array = $firstArray + $secondArray;

Upvotes: 1

Related Questions