Delete duplicate array completely?

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.

<?php

$ar1 = array(1, 2, 3, 4, 5, 6, 7); //Original array
$ar2 = array(2, 4, 8, 1, 9, 7); //Result array

$duplicates=array();
$non_duplicates=array();

foreach ($ar2 AS $value ) {
    if (in_array($value,$ar1)) {
        array_push($duplicates,$value);
    } else {
        array_push($non_duplicates,$value);
    }
}
var_dump($non_duplicates);
?>

I belive that array_push() is available in php version 5.2 but can’t be sure

@SpacePhoenix ;
WoW, what a nice trick.
I did not know array_push() can do that.

Thanks a lot man.


$ar3 =array_diff($ar2, $ar1);

Gives:
8
9

Note though that your function also checks for at least one array not to be empty, so if that is the spec you’d still have to do that check.

@Cups ;

Cool, the shorter the better.
Thanks.