PHP array_diff_ukey() Function
The array_diff_ukey() function in PHP compares the keys from array1 against the keys from array2 and returns the difference containing all the entries from array1 that are not present in any of the other arrays.
Syntax
array_diff_ukey ( array $array1 , array $array2 [, array $... ], callable $key_compare_func )
Parameter
array1(required)- This parameter signifies the input array to compare from.
array2(required)- This parameter signifies the input array to compare against.
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 returns an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.
Return
This function returns an array containing all the entries from array1 that are not present in any of the other arrays. It returns null if all the values of array1 are present in other arrays.
Example 1
$b) return 1; else return -1; } // initializing the Input Array 1 $array1 = array(01=>"Raj", 10=>"raj", 30=>"Atul"); // initializing the Input Array 2 $array2 = array(01=>"Mahesh", 40=>"Reema", 30=>"Ritu"); // comparing the keys with additional index check $difference=array_diff_ukey($array1, $array2, "my_function"); print_r($difference); ?>
Output
Array ( [10] => raj )
Example 2
$b)?1:-1;
}
$array1=array("1"=>"red","2"=>"green","3"=>"blue");
$array2=array("1"=>"red","5"=>"green","9"=>"blue");
$array3=array("3"=>"yellow","4"=>"red","7"=>"blue");
// comparing the keys of the arrays with additional index check
$difference=array_diff_ukey($array1,$array2,$array3,"myfunction");//return an array
print_r($difference);
?>
Output
Array ( [2] => green )
Example 3
$b) return 1; else return -1; } // initializing the Input Array 1 $array1 = array(01=>"Raj", 10=>"raj", 30=>"Atul"); // initializing the Input Array 2 $array2 = array(01=>"Raj", 10=>"raj", 30=>"Atul"); // comparing the keys with additional index check $difference=array_diff_ukey($array1, $array2, "my_function"); // will return NULL as both the arrays are same print_r($difference); ?>
Output
Array ( )
