PHP Programing language

array_diff() Functions in PHP
Previous Home Next
adplus-dvertising

array_diff()

The array_diff() function compares the values of two (or more) arrays, and returns the differences.

Syntax:
array_diff(array1,array2,array3...);

In above Syntax,"array1" compare from, "array2" compare against,"array3" compare against to more arrays.

Example:

<?php
$array1=array("a"=>"Apple","b"=>"Banana","c"=>"Orange","d"=>"Mango");
$array2=array("e"=>"Apple","f"=>"Banana","g"=>"Orange");
$result=array_diff($array1,$array2);
print_r($result);
?>

Output:

Array ( [d] => Mango )

array_diff_assoc()

The array_diff_assoc() function compares the keys and values of two (or more) arrays, and returns the differences.

Syntax:
array_diff_assoc(array1,array2,array3...);

In above Syntax,"array1" compare from, "array2" compare against,"array3" compare against to more arrays.

Example:

<?php
$array1=array("a"=>"Apple","b"=>"Banana","c"=>"Orange","d"=>"Mango");
$array2=array("e"=>"Apple","f"=>"Banana","g"=>"Orange");
$result=array_diff_assoc($array1,$array2);
print_r($result);
?>

Output:

Array ( [a] => Apple [b] => Banana [c] => Orange [d] => Mango )

array_diff_uassoc()

The array_diff_uassoc() function compares the keys and values of two (or more) arrays, and returns the differences.

Syntax:
array_diff_uassoc(array1,array2,array3...,myfunction);

In above Syntax,"array1" compare from, "array2" compare against,"array3" compare against to more arrays and "myfunction" is a string that define a callable comparison function. The comparison function must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument

Example:

<?php
function myfunction($a,$b)
{
if ($a===$b)
   {
   return 0;
   }
   return ($a>$b)?1:-1;
}

$array1=array("a"=>"Apple","b"=>"Banana","c"=>"Orange");
$array2=array("e"=>"Apple","f"=>"Banana","g"=>"Orange");
$result=array_diff_uassoc($array1,$array2,"myfunction");
print_r($result);
?>

Output:

Array ( [a] => Apple [b] => Banana [c] => Orange )

Previous Home Next