PHP array_udiff_assoc() Function

PHP array_udiff_assoc() Function

The array_udiff_assoc () function in PHP computes the difference between the specified arrays with additional index check and compares the data by a callback function. It returns all the values from array1 that are not present in any of the other arguments.

Syntax

array_udiff_assoc ( 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.

key_compare_func(required)- This parameter represents a string that describe a callable comparison function. The comparison function must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.

Return

This function returns an array containing all the values from array1 that are not present in any of the other arguments. It returns NULL if all the array elements are present in array1.

Example 1

$val2)?1:-1;
 }
 //input array1
 $array1=array("1"=>"PHP","2"=>"C#","3"=>"Java","4"=>"Python","5"=>"AI");
 //input array2 
 $array2=array("1"=>"Java","2"=>"C#","9"=>"Python");
 //input array2
 $array3=array("3"=>"Python","3"=>"Java","7"=>"AI","8"=>"C++");
 // Computes the difference of arrays with additional index check 
 $difference=array_udiff_assoc($array1,$array2,$array3,"compare_function");
 print_r($difference);
 ?> 

Output

Array
 (
     [1] => PHP
     [4] => Python
     [5] => AI
 ) 

Example 2

$b)? 1: 0; 
 } 
 // initializing the Input Array 1 
 $array1 = array(05=>"Reema", 10=>"raj", 30=>"Atul"); 
 // initializing the Input Array 2
 $array2 = array(20=>"Mahesh", 40=>"Smitha", 30=>"Ritu"); 
 // comparing the values with additional index check   
 $difference=array_udiff_assoc($array1, $array2, "compare_function"); 
 print_r($difference); 
 ?> 

Output

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

Example 3

 "Reema","2" => "Rama","3" => "Joe","4" => "Monica"); 
 $array2 = array( "1" => "Reema","2" => "Rama", "3" => "Chandler","4" => "Ross"); 
 // calculation the differences 
 $difference = array_udiff_assoc($array1,$array2, "Compare_Function"); 
 print_r($difference); //return "Joe","Monica"
 ?> 

Output

Array
 (
     [3] => Joe
     [4] => Monica
 )