0

I want to remove duplicate keys from both arrays.

My code is

$arr1[22068] = array('ID' => 22068);
$arr1[22067] = array('ID' => 22067);
$arr2[22068] = array('ID' => 22068);
$arr2[22066] = array('ID' => 22066);

$arr = array_diff($arr1, $arr2);

var_dump($arr); //It outputs null.

The final array should look like this--

$arr[22066] = array('ID' => 22066);
$arr[22067] = array('ID' => 22067);

Any help is highly appreciated.

5
  • 1
    I undertand for 22068, but why 22066 ? Commented Nov 28, 2016 at 22:21
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. Commented Nov 28, 2016 at 22:23
  • @CharlotteDunois I have updated the post. I want the key 22068 to be removed from both array and produce a new array. Commented Nov 28, 2016 at 22:31
  • easy with a foreach() loop, but maybe some one will answer with a sexier solution Commented Nov 28, 2016 at 22:36
  • I don't know the language, but could the problem involve that 1 - 1 = 0? Commented Nov 29, 2016 at 5:14

2 Answers 2

2

array_diff_key() is what you want.

$arr1[22068] = array('ID' => 22068);
$arr1[22067] = array('ID' => 22067);
$arr2[22068] = array('ID' => 22068);
$arr2[22066] = array('ID' => 22066);

// Get elements of array 1 which are not present in array 2
$unique_1 = array_diff_key($arr1, $arr2);

// Get elements of array 2 which are not present in array 1
$unique_2 = array_diff_key($arr2, $arr1);

// Merge unique values
$unique = $unique_1 + $unique_2;
Sign up to request clarification or add additional context in comments.

3 Comments

you should use $unique = $unique_1 + $unique_2; to preserve original numeric keys. array_merge will reset numeric keys 22067 and 22066 to 0 and 1
Brilliant. Thanks a lot for the solution and explanation too.
@krlv Thanks for pointing this out. I was missing the original key. Now it's fine.
1

So array_diff will give you what's different in array1 vs array2, and will NOT work on multi-dimensional arrays. You can switch to array_key_diff, however, you'll run into a similar issue:

$arr1[22068] = array('ID' => 22068);
$arr1[22067] = array('ID' => 22067);
$arr2[22068] = array('ID' => 22068);
$arr2[22066] = array('ID' => 22066);

$arr = array_diff_key($arr1, $arr2);

var_dump($arr); //It outputs array(1) {[22067]=> array(1) { ["ID"]=> int(22067) } }

I'm not aware of a "magic" solution with the differences, however, you do have options, you can take the code above and add an extra line for:

$arr2 = array_diff_key($arr2, $arr1);
var_dump($arr2)

then merge $arr and $arr2, or you can just write a loop going through and comparing each item. Depending on size, scope, readability, etc will depend on the actual set

1 Comment

Thanks for your help too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.