how to compare two array value and remove the duplicate value from the first array using php for example
JavaScript
x
$a = ['a','b','c','d','e','f'];
$b = ['a','e'];
$result_array = ['b','c','d','f'];
I tried this:
JavaScript
$a = ['a','b','c','d','e','f'];
$b = ['a','e'];
foreach($a as $key_a=>$val_a){
$val = '';
foreach($b as $key_b=>$val_b){
if($val_a != $val_b){
$val = $val_a;
}else{$val = $val_b;}
}
echo $val."<br>";
}
Advertisement
Answer
This is probably a duplicate, but I’m bored. Just compute the difference:
JavaScript
$a = array_diff($a, $b);
Or loop the main array, check for each value in the other and if found unset from the main array:
JavaScript
foreach($a as $k => $v) {
if(in_array($v, $b)) {
unset($a[$k]);
}
}
Or loop the other array, search for each value in the main array and use the key found to unset it:
JavaScript
foreach($b as $v) {
if(($k = array_search($v, $a)) !== false) {
unset($a[$k]);
}
}