array_uintersect_uassoc


php128 apg

COMPUTES the intersection of ARRAYS with additional index check, compares data and indexes by separate callback functions.





This function computes the intersection of ARRAYS with additional index check, compares data by a callback function.

The keys are used in the comparison unlike in the function array_uintersect.

The $value_compare_func, CALLBACK function, 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_uintersect_uassoc 
arr $array1 arr $array2 [, 
                                              
arr $arra3, ..., arr $arrayN ], 
                                                   callable 
$value_compare_func
                                                       callable 
$key_compare_func 
 

where,

$array1 The first ARRAY to compare from

$array2 
The second ARRAY to compare against

$array3 
The third ARRAY to compare against

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

$arrayN The last ARRAY to compare against

$value_compare_func 
The callable value function to use in the compare

$key_compare_func 
The callable key function to use in the compare

?>
 

$array1


The first ARRAY - to compare from.



$array2


The second ARRAY - to compare against.



$array3


The third ARRAY - to compare against.



$arrayN


The last ARRAY - to compare against.



$value_compare_func


The callback values comparison function.



$key_compare_func


The callback keys 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_commonA array_uintersect_uassoc($arr_current
                                                                  
$arr_gregory
                                                                  
$arr_thomas
                                                                 
'strcmp'
                                                                 
'strnatcasecmp'); 

$arr_commonB array_uintersect_uassoc($arr_current
                                                                  
$arr_gregory
                                                                  
$arr_thomas
                                                                 
'strnatcasecmp'
                                                                 
'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><brACCORDING ST. THOMAS AQUINAS:<br>'

print_r($arr_thomas); 

echo 
'<br><br><br>SEVEN DEADLY SINS - ( strcmp, strnatcasecmp ):<br>'

print_r($arr_commonA);

echo 
'<br><br><br>SEVEN DEADLY SINS - ( strnatcasecmp,  strcmp ):<br>'

print_r($arr_commonB);  

?> 

 RESULT   

SEVEN DEADLY SINS.

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

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

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


SEVEN DEADLY SINS - ( strcmp, strnatcasecmp ):
Array ( [1] => Lust )
[ Case-sensitive ]

SEVEN DEADLY SINS - ( strnatcasecmp, strcmp ):
Array ( [1] => Lust [2] => Pride )
[ Case-insensitive ]


  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_uintersect_uassoc($Acurr
                                                        
$Agreg
                                                        
$Athom
                                                       
'strnatcmp'
                                                       
'strnatcasecmp');  

$Bcomm array_uintersect_uassoc($Acurr
                                                        
$Agreg
                                                        
$Athom
                                                       
'strnatcasecmp'
                                                       
'strnatcmp');

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

print_r($Acurr);  

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

print_r($Agreg);  

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

print_r($Athom);  

echo 
'<br><br><br>SEVEN DEADLY SINS - ( strnatcmp, strnatcasecmp ):<br>';  

print_r($Acomm);

echo 
'<br><br><br>SEVEN DEADLY SINS - ( strnatcasecmp, strnatcmp ):<br>';  

print_r($Bcomm);   

?> 

 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 )


SEVEN DEADLY SINS - ( strnatcmp, strnatcasecmp ):
Array ( )


SEVEN DEADLY SINS - ( strnatcasecmp, strnatcmp ):
Array ( [first] => Lust )


  3 EXERCISE   

<?php

/*
* Function is implemented 
* in ext/standard/array.c
*/
class cr {
    private 
$priv_member;
    function 
__construct($val) {
        
$this->priv_member $val;
    }
    static function 
comp_func_cr($a$b) {
        if (
$a->priv_member === $b->priv_member) return 0;
        return (
$a->priv_member $b->priv_member) ? : -1;
    }
    static function 
comp_func_key($a$b) {
        if (
$a === $b) return 0;
        return (
$a $b) ? : -1;
    }
}
$a = array("0.1" => new cr(9), "0.5" => new cr(12), => new cr(23), => new cr(4), => new cr(-15),);
$b = array("0.2" => new cr(9), "0.5" => new cr(22), => new cr(3), => new cr(4), => new cr(-15),);
$result array_uintersect_uassoc($a$b, array("cr""comp_func_cr"), array("cr""comp_func_key"));
echo 
'<pre>';
var_dump($result);
echo 
'</pre>';

?>

  4 EXERCISE   

<?php

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

// Initialise function arguments 
// not being substituted (if any)
$array2 = array(12);

include(
'compare_function.inc');
$data_compare_func 'compare_function';
$key_compare_func 'compare_function';

//get an unset variable
$unset_var 10;
unset (
$unset_var);

// define some classes
class classWithToString
{
    public function 
__toString() {
        return 
"Class A object";
    }
}

class 
classWithoutToString
{
}

// heredoc string
$heredoc = <<<EOT
hello world
EOT;

// add arrays
$index_array = array (123);
$assoc_array = array ('one' => 1'two' => 2);

//array of values to iterate over
$inputs = array(

      
// int data
      
'int 0' => 0,
      
'int 1' => 1,
      
'int 12345' => 12345,
      
'int -12345' => -2345,

      
// float data
      
'float 10.5' => 10.5,
      
'float -10.5' => -10.5,
      
'float 12.3456789000e10' => 12.3456789000e10,
      
'float -12.3456789000e10' => -12.3456789000e10,
      
'float .5' => .5,

      
// null data
      
'uppercase NULL' => NULL,
      
'lowercase null' => null,

      
// boolean data
      
'lowercase true' => true,
      
'lowercase false' =>false,
      
'uppercase TRUE' =>TRUE,
      
'uppercase FALSE' =>FALSE,

      
// empty data
      
'empty string DQ' => "",
      
'empty string SQ' => '',

      
// string data
      
'string DQ' => "string",
      
'string SQ' => 'string',
      
'mixed case string' => "sTrInG",
      
'heredoc' => $heredoc,

      
// object data
      
'instance of classWithToString' => new classWithToString(),
      
'instance of classWithoutToString' => new classWithoutToString(),

      
// undefined data
      
'undefined var' => @$undefined_var,

      
// unset data
      
'unset var' => @$unset_var,
);

// loop through each element of the array for array1

foreach($inputs as $key =>$value) {
    echo 
"<br><br>$key:<br>";
    try {
        
var_dumparray_uintersect_uassoc($value$array2$data_compare_func$key_compare_func) );
    } catch (
TypeError $e) {
        echo 
$e->getMessage(), "<br>";
    }
};

?>

  5 EXERCISE   

<?php

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

// Initialise function arguments 
// not being substituted (if any)
$array1 = array(12);

include(
'compare_function.inc');
$data_compare_func 'compare_function';
$key_compare_func 'compare_function';

//get an unset variable
$unset_var 10;
unset (
$unset_var);

// define some classes
class classWithToString
{
    public function 
__toString() {
        return 
"Class A object";
    }
}

class 
classWithoutToString
{
}

// heredoc string
$heredoc = <<<EOT
hello world
EOT;

// add arrays
$index_array = array (123);
$assoc_array = array ('one' => 1'two' => 2);

//array of values to iterate over
$inputs = array(

      
// int data
      
'int 0' => 0,
      
'int 1' => 1,
      
'int 12345' => 12345,
      
'int -12345' => -2345,

      
// float data
      
'float 10.5' => 10.5,
      
'float -10.5' => -10.5,
      
'float 12.3456789000e10' => 12.3456789000e10,
      
'float -12.3456789000e10' => -12.3456789000e10,
      
'float .5' => .5,

      
// null data
      
'uppercase NULL' => NULL,
      
'lowercase null' => null,

      
// boolean data
      
'lowercase true' => true,
      
'lowercase false' =>false,
      
'uppercase TRUE' =>TRUE,
      
'uppercase FALSE' =>FALSE,

      
// empty data
      
'empty string DQ' => "",
      
'empty string SQ' => '',

      
// string data
      
'string DQ' => "string",
      
'string SQ' => 'string',
      
'mixed case string' => "sTrInG",
      
'heredoc' => $heredoc,

      
// object data
      
'instance of classWithToString' => new classWithToString(),
      
'instance of classWithoutToString' => new classWithoutToString(),

      
// undefined data
      
'undefined var' => @$undefined_var,

      
// unset data
      
'unset var' => @$unset_var,
);

// loop through each element of the array for array2

foreach($inputs as $key =>$value) {
    echo 
"<br><br>$key:<br>";
    try {
        
var_dumparray_uintersect_uassoc($array1$value$data_compare_func$key_compare_func) );
    } catch (
TypeError $e) {
        echo 
$e->getMessage(), "<br>";
    }
};

?>

  6 EXERCISE   

<?php

echo "Testing array_uintersect_uassoc() : usage variation - incorrect callbacks.";

$arr1 = array(1);
$arr2 = array(1,2);

echo 
"<br><br>Comparison function with an incorrect return value:<br>";
function 
incorrect_return_value ($val1$val2) {
  return array(
1);
}
var_dump(array_uintersect_uassoc($arr1$arr2'incorrect_return_value''incorrect_return_value'));

echo 
"<br><br>Comparison function taking too many parameters:<br>";
function 
too_many_parameters ($val1$val2$val3) {
  return 
1;
}
try {
    
var_dump(array_uintersect_uassoc($arr1$arr2'too_many_parameters''too_many_parameters'));
} catch (
Throwable $e) {
    echo 
"Exception: " $e->getMessage() . "<br>";
}

echo 
"<br><br>Comparison function taking too few parameters:<br>";
function 
too_few_parameters ($val1) {
  return 
1;
}
var_dump(array_uintersect_uassoc($arr1$arr2'too_few_parameters''too_few_parameters'));

?>