PHP Tutorial - PHP array_uintersect() Function






Definition

The array_uintersect() function uses a user-defined function to compare the values of two or more arrays, and returns the matches.

Syntax

PHP array_uintersect() Function has the following syntax.

array_uintersect(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.Comparison function name.

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





Example 1

uses a user-defined function to compare the values of two arrays, and returns the matches


<?php/* w w w.ja  v  a  2 s .  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"=>"HTML","e"=>"PHP");

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

The code above generates the following result.





Example 2

uses a user-defined function to compare the values of three arrays, and returns the matches


<?php/*w  w w. j a v a  2s.  co  m*/
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");
$a3=array("a"=>"At","b"=>"CSS","X","Y");

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

The code above generates the following result.