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
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.
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_uassoc($myarrayassoc1, $myarrayassoc2, user_fn); //Print result print_r($newArr); ?> |
Output
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.
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_uassoc($myarrayassoc1, $myarrayassoc2, $myarrayassoc3, user_fn); //Print result print_r($newArr); ?> |
Output
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