Last Updated on August 13, 2021 by Roshan Parihar
PHP array_diff_ukey() Function is used to compare only the keys of two or more arrays using the user-defined function. After the comparison, it returns the difference of the first array with the rest of the arrays.
It compares the keys of the first array (array1) with the keys of the second array (array2) and the rest of the arrays in PHP. After that, it returns the keys of the first array (array1) that are not present in array2 and the rest of the arrays.
Let’s find out the use of the PHP array_diff_ukey()
function with the examples given below.
Syntax of PHP array_diff_ukey() Function
The array_diff_ukey()
function takes comma-separated arrays and a user-defined function as an argument.
Description of Parameters
Name | Description |
---|---|
array1 | Required. Specify the first array to compare with the rest of the arrays. |
array2 | Required. It is the second array to compare against. |
array3…arrayN | optional. More arrays to compare against. |
user_defined_function | Required. Specify the user-defined function to compare the keys. |
Let’s find out the use of the PHP array_diff_ukey()
Function with the examples given below.
Examples of PHP array_diff_ukey() Function
See the different examples given below to learn comparing the array keys using the array_diff_ukey()
Function in PHP.
Example 1: Compare the Difference of Keys of Two Arrays in PHP
Suppose that, you have two arrays to compare the difference of keys with user-defined functions. The PHP array_diff_ukey()
function compares the keys of the first array with the keys of the second array. After comparing the keys, it returns the difference of keys of the first array that are not present in the second array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php //Declare an associative array in PHP $myarrayassoc1 = array("Cycle" => 2, "Bike" => 5, "Car" => 9, "Ricksaw" => 4); $myarrayassoc2 = array("Cycle" => 2, "Bike" => 5, "Bolero" => 11, "Ricksaw" => 5); //Define user function to compare keys function user_fn($a, $b){ //compare if they are equal if($a == $b){ return 0; } //compare if one is greater than other return($a > $b)? 1 : -1; } //Compare the differrence of keys and values in array $newArr = array_diff_ukey($myarrayassoc1, $myarrayassoc2, user_fn); //Print result print_r($newArr); ?> |
Output
The output shows that the key ‘Car’ is present in the second array without considering the values. The key ‘Bolero’ is also a different key but the function prefers the first array to give the result.
Example 2: Get the Difference of Keys Among More than Two Arrays in PHP
In addition to the above example, you can also compare the keys of more than two arrays using the PHP array_diff_ukey()
function. It returns the difference of the keys and uses the user-defined function without considering the values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php //Declare an associative array in PHP $myarrayassoc1 = array("Cycle" => 2, "Bike" => 5, "Car" => 9, "Ricksaw" => 4, "WagonR" => 7); $myarrayassoc2 = array("Cycle" => 6, "Bike" => 5, "Bolero" => 11, "Ricksaw" => 13); $myarrayassoc3 = array("Cycle" => 2, "Bike" => 9, "Bolero" => 22, "Car" => 9); //Define user function to compare keys function user_fn($a, $b){ //compare if they are equal if($a == $b){ return 0; } //compare if one is greater than other return($a > $b)? 1 : -1; } //Get the differrence among more that two arrays $newArr = array_diff_ukey($myarrayassoc1, $myarrayassoc2, $myarrayassoc3, user_fn); //Print result print_r($newArr); ?> |
Output
The above example shows that only a single key ‘WagonR’ is not present in the second and the third arrays. It compares the keys and finds it not matching with the second and the rest of the arrays.
You May Also Like to Read