Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP array_diff_ukey() Function.
The array_diff_ukey() function compares only the keys of two or more arrays using a user defined function and returns an array containing the entries from array1 that are not present in array2, array3, etc.
This function is different from array_diff() function because the array_diff() function compares the values while this function compares the keys.
This PHP function is different from array_diff_assoc() because the PHP array_diff_assoc() function makes use of the internal algorithm to compare the keys and their values whereas this function makes use of the user defined functions.
The array_diff_ukey() function compares only the keys of two or more arrays using a user defined function and returns an array containing the entries from array1 that are not present in array2, array3, etc.
This function is different from array_diff() function because the array_diff() function compares the values while this function compares the keys.
This PHP function is different from array_diff_assoc() because the PHP array_diff_assoc() function makes use of the internal algorithm to compare the keys and their values whereas this function makes use of the user defined functions.
Syntax
Following below is the syntax to use this function -
array_diff_ukey ( $array1, $array2 [, $array3...,callback $key_compare_func] );
Parameter Details
Sr.No | Parameter & Description |
---|---|
1 | array1(Required) The first array is the array that the others will be compared with. |
2 | array2(Required) It is an array to be compared with the first array |
3 | array3(Optional) It is an array to be compared with the first array |
4 | key_compare_func(Required) It is a callback function to use to compare the keys. This callback function must return an integer <, =, > than 0 if the first argument is considered to be respectively <, =, > than the second argument. |
Return Value
This built-in PHP function returns an array containing all the entries from array1 that are not present in other arrays.
PHP Version
This function was first lunched as part of core PHP v 5.1.0.
Example1
Try out the below example -
<?php function key_compare_func($a, $b) { if ($a === $b) { return 0; } return ($a > $b)? 1: -1; } $input1 = array(0=>"banana", 1=>"orange", 2=>"grapes"); $input2 = array(3=>"apple",1=>"apricot", 5=>"mango"); print_r(array_diff_ukey($input1,$input2,"key_compare_func")); ?>
Output
When the above code is executed, it will produce the following result -
Array ( [0] => banana [2] => grapes )
Example2
Try the following example -
<?php function key_compare_func($a, $b) { if ($a === $b) { return 0; } return ($a > $b)? 1: -1; } $input1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4); $input2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8); print_r(array_diff_ukey($input1,$input2,"key_compare_func")); ?>
Output
When the above code is executed, it will produce the following result -
Array ( [red] => 2 [purple] => 4 )
Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP array_fill() Function.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.