Home > Article > Backend Development > How to learn PHP array_diff_uassoc()_PHP tutorial
Definition and usage
The array_diff_uassoc() function uses a user-defined callback function to do index checking to calculate the difference between two or more arrays. Returns an array containing the values in array1 but not in any of the other argument arrays.
Note that unlike the array_diff() function, key names are also compared.
The parameter function is a user-defined function used to compare two arrays. The function must take two parameters - namely, the two key names to be compared. So the behavior is exactly opposite to the function array_diff_assoc(), which uses an internal function for comparison.
The key names in the returned array remain unchanged.
Grammar
array_diff_uassoc(array1,array2,array3...,function)
Parameters
Description
array1 required. The first array to compare with other arrays.
array2 required. The array to compare to the first array.
array3 is optional. The array to compare to the first array. There can be multiple.
function required. The name of the user-defined function.
Example 1
$v2) { return 1; } else { return -1 ; } } $a1=array(0=>"Dog",1=>"Cat",2=>"Horse"); $a2=array(3=>"Dog",1=> "Cat",5=>"Horse"); print_r(array_diff_uassoc($a1,$a2,"myfunction")); ?>
Output:
Array ( [0] => Dog [2] => Horse )
Example 2
How to allocate multiple arrays to this function:
$v2) { return 1; } else { return -1 ; } } $a1=array(0=>"Dog",1=>"Cat",2=>"Horse"); $a2=array(3=>"Dog",1=> "Cat",5=>"Horse"); $a3=array(6=>"Bird",0=>"Dog",5=>"Horse"); print_r(array_diff_uassoc($a1, $a2,$a3,"myfunction")); ?>
Output:
Array ( [2] => Horse )