array_intersect_uassoc


php128 apg

COMPUTES the intersection of ARRAYS with additional index check, compares indexes by a callback function.





This function returns an ARRAY containing all the values from $array1 that are present in any of the other arrays.

Multiple occurrences in $array1 are all treated the same way.

Note that the keys are also used in the comparison unlike in the function array_intersect.

$key_compare_func must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

Prior to PHP 7.0.0 this value is an INTEGER in the range -2147483648 to 2147483647.



<?php

arr array_intersect_uassoc 
arr $array1 arr $array2 [,
               
arr $arra3, ..., arr $arrayN ], callable $key_compare_func ) )
 

where,

$array1 The first ARRAY to check

$array2 
The second ARRAY to compare against

$array3 
The third ARRAY to compare against

. . . . . . . . . . . . . . . .

$arrayN The last ARRAY to compare against

$key_compare_func 
The function used in the comparisson

?>
 

$array1


The first ARRAY - values to check.



$array2


The second ARRAY - to compare values against.



$array3


The third ARRAY - to compare values against.



$arrayN


The last ARRAY - to compare values against.



$key_compare_func


The callback comparison function.



  1 EXERCISE   

<?php

$arr_current 
= [ "Lust""Pride"
                             
"Gluttony""Greed"
                             
"Sloth""Wrath""Envy"];

$arr_gregory = [ "Lust""Pride"
                         
"Envy""Anger"
                         
"Sadness""Avarice""Gluttony" ];

$arr_thomas = [ "Lust","PRIDE""Greed"
                    
"Gluttony""Sloth""Envy""Anger" ];

$arr_common array_intersect_uassoc($arr_current
                                                              
$arr_gregory
                                                              
$arr_thomas
                                                             
'strcmp');

echo 
'SEVEN DEADLY SINS.<br><br>CURRENT:<br>';

print_r($arr_current);

echo 
'<br><br>ACCORDING POPE GREGORY:<br>';

print_r($arr_gregory);

echo 
'<br><br>ACCORDING ST. THOMAS AQUINAS:<br>';

print_r($arr_thomas);

echo 
'<br><br><br>COMMON AT ALL:<br>';

print_r($arr_common);

?> 

 RESULT   

SEVEN DEADLY SINS.

CURRENT:
Array ( [0] => Lust [1] => Pride [2] => Gluttony [3] => Greed [4] => Sloth [5] => Wrath [6] => Envy )

ACCORDING POPE GREGORY:
Array ( [0] => Lust [1] => Pride [2] => Envy [3] => Anger [4] => Sadness [5] => Avarice [6] => Gluttony )

ACCORDING ST. THOMAS AQUINAS:
Array ( [0] => Lust [1] => PRIDE [2] => Greed [3] => Gluttony [4] => Sloth [5] => Envy [6] => Anger )


COMMON AT ALL:
Array ( [0] => Lust )

Elements are part of all ARRAYS.

The parity between keys and values was maintained.

The strcmp function was used as callback which is case-sensitive.


  2 EXERCISE   

<?php

$Acurr 
= [ 'first' => "Lust"'second' => "Gluttony"
           
'third' => "Greed"'fourth' => "Sloth"'fifth' => "Wrath"
                       
'sixth' => "Envy"'seventh' => "Pride"]; 

$Agreg = [ 'first' => "Lust"'second' => "Envy",  
                  
'third' => "Anger"'fourth' => "Sadness",  
                  
'fifth' => "Avarice"'sixth' => "Gluttony",  
                               
'seventh' => "Pride" ]; 

$Athom = [ 'first' => "Lust"'second' => "Greed",  
                        
'third' => "Gluttony"'fourth' => "Pride",  
                        
'fifth' => "Sloth"'sixth' => "Envy"
                                      
'seventh' => "Anger" ]; 

$Acomm array_intersect_uassoc($Acurr
                                                      
$Agreg
                                                      
$Athom
                                                     
'strcmp');

echo 
'SEVEN DEADLY SINS.<br><br>CURRENT:<br>';

print_r($Acurr);

echo 
'<br><br>ACCORDING POPE GREGORY:<br>';

print_r($Agreg);

echo 
'<br><br>ACCORDING ST. THOMAS AQUINAS:<br>';

print_r($Athom);

echo 
'<br><br><br>COMMON AT ALL:<br><br>';

print_r($Acomm);

?> 

 RESULT   

SEVEN DEADLY SINS.

CURRENT:
Array ( [first] => Lust [second] => Gluttony [third] => Greed [fourth] => Sloth [fifth] => Wrath [sixth] => Envy [seventh] => Pride )

ACCORDING POPE GREGORY:
Array ( [first] => Lust [second] => Envy [third] => Anger [fourth] => Sadness [fifth] => Avarice [sixth] => Gluttony [seventh] => Pride )

ACCORDING ST. THOMAS AQUINAS:
Array ( [first] => Lust [second] => Greed [third] => Gluttony [fourth] => Pride [fifth] => Sloth [sixth] => Envy [seventh] => Anger )


COMMON AT ALL:
Array ( [first] => Lust )

Elements are part of all ARRAYS.

The parity between keys and values was maintained.

The strcmp function was used as callback which is case-sensitive.


  3 EXERCISE   

<?php

/*
* Function is implemented in ext/standard/array.c
*/

function key_compare_func($a$b) {
    if (
$a === $b) {
        return 
0;
    }
    return (
$a $b) ? : -1;
}
$array1 = array("a" => "green""b" => "brown"
                                   
"c" => "blue""red");
$array2 = array("a" => "green""yellow""red");

echo 
'The given $array1:<br>';
var_dump($array1);

echo 
'<br><br>The given $array2:<br>';
var_dump($array2);


echo 
'<br><br><br>The $result:<BR>';
$result array_intersect_uassoc($array1
                                           
$array2
                                           
"key_compare_func");
var_dump($result);

?>

  4 EXERCISE   

<?php

echo "Testing array_intersect_uassoc() : usage variation.";

//Initialize variables
$arr_default_int = array(1);
$arr_float = array(=> 1.001.00 => 2.002.00 => 3.00);
$arr_string = array('1''2''3');
$arr_string_float = array('1.00''2.00');


echo 
'<br><br>The given $arr_default_int:<br>';
var_dump($arr_default_int);

echo 
'<br><br>The given $arr_float:<br>';
var_dump($arr_float);

echo 
'<br><br>The given $arr_string:<br>';
var_dump($arr_string);

echo 
'<br><br>The given $arr_string_float:<br>';
var_dump($arr_string_float);

function 
key_compare_func($a$b)
{
    if (
$a === $b) {
        return 
0;
    }
    return (
$a $b)? 1:-1;
}

echo 
"<br><br><br>Result of integers 
                          and floating point intersection:<br>"
;
var_dumparray_intersect_uassoc($arr_default_int
                                                       
$arr_float
                                                      
"key_compare_func") );

echo 
"<br><br>Result of integers 
                    and strings containing integers intersection:<br>"
;
var_dumparray_intersect_uassoc($arr_default_int
                                                      
$arr_string
                                                     
"key_compare_func") );

echo 
"<br><br>Result of integers 
         and strings containing floating points intersection:<br>"
;
var_dumparray_intersect_uassoc($arr_default_int
                                                       
$arr_string_float
                                                      
"key_compare_func") );
?>

  5 EXERCISE   

<?php

echo "Testing array_intersect_uassoc() : usage variation.";

//Initialize variables
$arr_float = array(=> 1.001.00 => 2.00);
$arr_string = array('1''2''3');
$arr_string_float = array('1.00''2.00');

echo 
'<br><br>The given $arr_float:<br>';
var_dump($arr_float);

echo 
'<br><br>The given $arr_string:<br>';
var_dump($arr_string);

echo 
'<br><br>The given $arr_string_float:<br>';
var_dump($arr_string_float);

function 
key_compare_func($a$b)
{
    if (
$a === $b) {
        return 
0;
    }
    return (
$a $b)? 1:-1;
}

echo 
"<br><br><br>Result of 
        floating points and 
               strings containing integers intersection:<br>"
;
var_dumparray_intersect_uassoc($arr_float
                                                       
$arr_string
                                                      
"key_compare_func") );

echo 
"<br><br>Result of 
        floating points and 
                 strings containing floating point intersection:<br>"
;
var_dumparray_intersect_uassoc($arr_float
                                                       
$arr_string_float
                                                      
"key_compare_func") );
?>

  6 EXERCISE   

<?php

echo "Testing array_intersect_uassoc() : usage variation.";

//Initialize variables
$arr1_string_int = array('1''2');
$arr2_string_int = array('1''3');
$arr1_string_float = array('1.00''2.00');
$arr2_string_float = array('1.00''3.00');

echo 
'<br><br>The given $arr1_string_int:<br>';
var_dump($arr1_string_int);

echo 
'<br><br>The given $arr2_string_int:<br>';
var_dump($arr2_string_int);

echo 
'<br><br>The given $arr1_string_float:<br>';
var_dump($arr1_string_float);

echo 
'<br><br>The given $arr2_string_float:<br>';
var_dump($arr2_string_float);

function 
key_compare_func($a$b)
{
    if (
$a === $b) {
        return 
0;
    }
    return (
$a $b)? 1:-1;
}

echo 
"<br><br><br>Result of 
          strings containing integers intersection:<br>"
;
var_dumparray_intersect_uassoc($arr1_string_int
                                                        
$arr2_string_int
                                                       
"key_compare_func") );

echo 
"<br><br>Result of 
          strings containing floating points intersection.<br>"
;
var_dumparray_intersect_uassoc($arr1_string_float
                                                        
$arr2_string_float
                                                       
"key_compare_func") );

echo 
"<br><br>Result of 
          strings containing integers and 
            strings containing floating points intersection:<br>"
;
var_dumparray_intersect_uassoc($arr1_string_int
                                                       
$arr2_string_float
                                                      
"key_compare_func") );
?>

  7 EXERCISE   

<?php

echo "Testing array_intersect_uassoc() : usage variation.";

//Initialize variables
$ref_var 'a';
$array1 = array('a'$ref_var);
$array2 = array('a' => 1, &$ref_var);

echo 
'<br><br>The given $array1:<br>';
var_dump($array1);

echo 
'<br><br>The given $array2:<br>';
var_dump($array2);


echo 
"<br><br><br>With referenced variable 
                    \$ref_var has value '
$ref_var':<br>";
var_dumparray_intersect_uassoc($array1
                                                       
$array2
                                                      
"strcasecmp") );

// re-assign reference variable to different value
$ref_var 10;
echo 
"<br><br>With referenced variable
                    \$ref_var value changed to: 
$ref_var:<br>";
var_dumparray_intersect_uassoc($array1
                                                      
$array2
                                                     
"strcasecmp") );

//When array are referenced
$array2 = &$array1;
echo 
"<br><br>When \$array2 is referencd to \$array1:<br>";
var_dumparray_intersect_uassoc($array1
                                                      
$array2
                                                     
"strcasecmp") );
?>