PHP array_diff_uassoc() Function

Last Updated on August 13, 2021 by Roshan Parihar

PHP array_diff_uassoc() Function is used to compare the keys and values of two or more arrays using the user-defined function. It returns the difference after comparing the keys and values of the first array with the rest of the arrays.

It compares the first array (array1) with the second array (array2) and rest of the arrays in PHP. After that, it returns the keys of the first array (array1) that are not present in array2 and other arrays.

Let’s find out the use of the PHP array_diff_uassoc() function with the examples given below.

Syntax of PHP array_diff_uassoc() Function

array_diff_uassoc(array1, array2, array3, …arrayN, user_defined_function)

The array_diff_uassoc() function takes comma-separated arrays and the user defined function as the argument.

Description of Parameters

Name Description
array1 Required. Specify the first array to compare with the other arrays.
array2 Required. It the second array to compare against.
array3…arrayN optional. Specify the more arrays to compare against.
user_defined_function Required. It the user defined function to compare the keys.

Let’s find out the use of the PHP array_diff_uassoc() Function with the examples given below.

Examples of PHP array_diff_uassoc() Function

See the different types of examples to learn comparing the array keys and values using the array_diff_uassoc() Function in PHP.

Example 1: Compare the Difference of Keys and Values of Two Arrays in PHP

Suppose that, you have two arrays to compare the keys and values with user defined function that compares keys. The PHP array_diff_uassoc() Function compares the keys and values of the first array with the the second array. After that, it returns the difference of keys and values of the first array that are not present in the second array.

Output

Array ( [Car] => 9 [Ricksaw] => 4 )

The output shows that the key and values of ‘Car’ and ‘Ricksaw’ are not present in the second array. The key ‘Ricksaw’ is present in the second array but with different value. That’s why it also returns the key ‘Ricksaw’ in the output.

Example 2: Get the Difference of Keys and Values Among More than Two Arrays in PHP

In addition to the above example, you can also compare more than two arrays according to their keys and values using the PHP array_diff_uassoc() function. It returns the difference of the keys and values and uses the user defined function to compare them.

Output

Array ( [Ricksaw] => 4 [WagonR] => 7 )

The above example shows that the two elements with keys ‘Ricksaw’ and ‘WagonR’ are not present in the second and third arrays. It compares the keys and values and finds them not matching with the first with rest of the other 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.