Yo,
I’m trying to compare and remove duplicate element (value) from two different arrays.
//Call jaz_remove_duplicate_array
function rda() {
$ar1 = array(1, 2, 3, 4, 5, 6, 7); //Original array
$ar2 = array(2, 4, 8, 1, 9, 7); //Result array
$this->jaz_remove_duplicate_array($ar1, $ar2);
}
//Merge array and remove duplicate
function jaz_remove_duplicate_array($ar1, $ar2) {
echo 'Array 1<br>';
foreach($ar1 as $key => $value){
echo $key . '-' . $value .'<br>';
}
echo '<br>';
echo 'Array 2<br>';
foreach($ar2 as $key => $value){
echo $key . '-' . $value .'<br>';
}
//check if base array is empty.
if(count($ar1) < 1) {
return FALSE;
}
//Else continue removing value if duplicate value found.
reset($ar2);
echo '<br>Looping: <br>';
foreach($ar2 as $key2 => $value2) {
if (in_array($value2, $ar1)) {
//If duplicate found, delete duplicate on base array (array 1).
unset($ar1[$value2]);
}
}
//echo $value1 .'<br>';
echo '<br>Result: <br>';
print_r($ar1);
}
I tried array_unique(), but not the exact method I’m looking.
I tried googling this but the solution I found needs higher version of PHP 5.4.
I need a solution that will work under PHP 5.2.
My codes above it seems to work, but I guess it needs more tweaking.
For the example above it suppose to output 8 and 9 since these numbers has no duplicate.
Thanks in advance.