PHP array_udiff() Function

PHP array_udiff() Function

The array_udiff() function in PHP calculates the difference of arrays by using a callback function for data comparison.

Syntax

array_udiff ( array $array1 , array $array2 [, array $... ], callable $value_compare_func )

Parameter

array1(required)- This array represents the first input array.

array2(required)- This array represents the second input array.

array3….(optional)- It represents more arrays to compare against.

value_compare_func(required)- This parameter represents a string that describe a callable comparison function. The comparison function returns an integer greater(<), equal to(=), or less than( >) than 0 if the first argument is greater(<), equal to(=), or less than( >) than the second argument.

Return

This function returns an array containing all the values of the first input array that does not exists in any of the other given arrays.

Example 1

 "mango", "b" => "Grapes", "c" => "Guava", "orange");
 //initializing the input array2
 $array2 = array("a" => "Mango", "c" => "GRAPES", "yellow", "orange");
 echo("The array_uintersect_assoc() returns:\n ");
 //returns an array containing all the values of array1 that are not present in all the other given arrays
 //will check only the values nor the keys 
 $differnce=array_udiff ($array1, $array2, "strcasecmp");//case-insensitive
 //will return Guava
 print_r($differnce);
 ?> 

Output

The array_uintersect_assoc() returns:
  Array
 (
     [c] => Guava
 ) 

Example 2

 "green", "2" => "brown", "3" => "blue", "red");
 //initializing the input array2
 $array2 = array("1" => "GREEN", "2" => "brown", "yellow", "red");
 echo("The array_udiff() returns:\n ");
 //returns an array containing all the values of array1 that are not present in all the other given arrays
 print_r(array_udiff ($array1, $array2, "strcasecmp"));
 ?> 

Output

The array_udiff() returns:
  Array
 (
     [3] => blue
 ) 

Example 3

$val2)?1:-1;
 }
 //input array1
 $array1=array("a"=>"PHP","b"=>"C#","c"=>"C++","d"=>"Python","e"=>"AI");
 //input array2
 $array2=array("a"=>"PHP","b"=>"C#","c"=>"Python");
 //input array2
 $array3=array("a"=>"PHP","c"=>"Java","d"=>"AI","e"=>"C++"); 
 // returns the difference of the three given arrays with the help of callable functions
 $diference=array_udiff ($array1,$array2,$array3,"myfunction");
 //will return null
 print_r($diference);
 ?> 

Output

Array
 (
 ) 

Example 4

$b)? 1 : -1;  
 } 
 // initializing the Input Array 1 
 $array1 = array(05=>"Reema", 10=>"raj", 30=>"Atul"); 
 // initializing the Input Array 2
 $array2 = array(05=>"Mahesh", 40=>"Smitha", 30=>"Atul"); 
 //returns the difference of the three given arrays with the help of value callable functions
 $difference=array_udiff ($array1, $array2, "myfunction"); 
 print_r($difference); 
 ?> 

Output

Array
 (
     [5] => Reema
     [10] => raj
     [30] => Atul
 )