PHP Tutorial - PHP array_udiff_assoc() Function






Definition

The array_udiff() function use a built-in function to compare the keys and values of two or more arrays, and returns the differences.

Syntax

PHP array_udiff_assoc() Function has the following syntax.

array_udiff_assoc(array1,array2,array3...,myfunction)

Parameter

ParameterIs RequiredDescription
array1Required.Array to compare from
array2Required.Array to compare against
array3,...Optional.More arrays to compare against
myfunctionRequired.A string that define a callable comparison function.

myfunction must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.





Example

Use a built-in function to compare the keys and values of two or more arrays, and returns the differences


<?php/*from  w w  w  .  j  a v  a  2s .com*/
function myfunction($a,$b){
    if ($a===$b){
      return 0;
    }
    return ($a>$b)?1:-1;
}

$a1=array("a"=>"A","b"=>"HTML","c"=>"java2s.com");
$a2=array("a"=>"Apple","b"=>"CSS","e"=>"PHP");

$result=array_udiff_assoc($a1,$a2,"myfunction");
print_r($result);
?>

The code above generates the following result.