PHP Tutorial - PHP array_uintersect_uassoc() Function






Definition

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

Syntax

PHP array_uintersect_uassoc() Function has the following syntax.

array_uintersect_uassoc(array1,array2,array3...,myfunction_key,myfunction_value)

Parameter

ParameterIs RequiredDescription
array1Required.The array to compare from
array2Required.An array to compare against
array3,...Optional.More arrays to compare against
myfunction_keyRequired.function name to compare keys.
myfunction_valueRequired.function name to compare values.

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





Example

Use two user-defined functions to compare the keys and values of two or more arrays, and returns the matches


<?php/*from  w  ww  . java 2s . c  om*/
    function myfunction_key($a,$b){
        if ($a===$b){
          return 0;
        }
        return ($a>$b)?1:-1;
    }
    
    function myfunction_value($a,$b){
        if ($a===$b){
          return 0;
        }
        return ($a>$b)?1:-1;
    }
    
    $a1=array("a"=>"A","b"=>"B","c"=>"C");
    $a2=array("a"=>"A","b"=>"B","c"=>"java2s.com");
    
    $result=array_uintersect_uassoc($a1,$a2,"myfunction_key","myfunction_value");
    print_r($result);
?>

The code above generates the following result.