PHP array_diff_ukey() Function

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

array_diff_ukey(array1, array2, array3, …arrayN, user_defined_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.

Output

Array ( [Car] => 9 )

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.

Output

Array ( [WagonR] => 7 )

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

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.