Meryvn
Meryvn

Reputation: 65

Compare 2 arrays remove the duplicates that appear in both in php

I have two arrays, I want to remove the duplicate(the values that appear in both) And remain with values that are not in Array A. For example I have array Master_arr and Log_arr. I want to remove the duplicates that appear in both and add in master_arr values that dont exist from log_arr. Master_arr contains CAT,DOG,RABBIT and Log_arr contains CAT,DOG,RABBIT,CAR,PHONE. I want to add CAR and PHONE to Master_arr.

$master_arr = array(CAT,DOG,RABBIT);
$log_arr = array(CAT,CAR,RABBIT,DOG,PHONE);
$unique=array_unique( array_merge($master_arr, $log_arr) );
print_r($unique);

Upvotes: 0

Views: 5273

Answers (2)

Manoj Purohit
Manoj Purohit

Reputation: 3453

try this

$master_arr = array(CAT,DOG,RABBIT);
$log_arr = array(CAT,CAR,RABBIT,DOG,PHONE);
$unique=array_unique( array_merge($master_arr, $log_arr) );
$master_arr=array_diff($unique, $master_arr);
print_r($master_arr);

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191829

I believe you want

$unique = array_diff(array_merge($master_arr, $log_arr), $master_arr);

Upvotes: 0

Related Questions