Compare two arrays with built-in function to compare keys and a user function to compare values and return the differences in PHP
Description
The following code shows how to compare two arrays with built-in function to compare keys and a user function to compare values and return the differences.
Example
//from w ww. j a va 2s . c o m
<?php
function myfunction($a,$b){
if ($a===$b){
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","b"=>"blue","c"=>"green");
$result=array_udiff_assoc($a1,$a2,"myfunction");
print_r($result);
?>
The code above generates the following result.