array_uintersect_assoc


php128 apg

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





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_assoc 
arr $array1arr $array2 [, 
                                             
arr $arra3, ..., arr $arrayN ], 
                                                 callable 
$value_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 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.



  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_assoc($arr_current
                                                                
$arr_gregory
                                                                
$arr_thomas
                                                               
'strcmp'); 

$arr_commonB array_uintersect_assoc($arr_current
                                                                
$arr_gregory
                                                                
$arr_thomas
                                                               
'strnatcasecmp'); 

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>SEVEN DEADLY SINS - ( strcmp ):<br>'

print_r($arr_commonA);

echo 
'<br><br>SEVEN DEADLY SINS - ( strnatcasecmp ):<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 ):
Array ( [1] => Lust )
[ Case-sensitive ]

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

Keys and values of the first array were checked against the others.

All keys and values associations have been verified.


  2 EXERCISE   

<?php

echo "Testing array_uintersect_assoc() : basic functionality -
           testing with multiple array arguments."
;

include(
'compare_function.inc');
$data_compare_function 'compare_function';

// Initialise all required variables
$arr1 = array("one" => "one""02" => "two"
                           
'3' => "three""four"
                       
"0.5" => 50.6 => 6"0x7" => "seven");
$arr2 = array("one" => "one""02" => "two"'3' => "three");
$arr3 = array("one" => "one"'3' => "three""0.5" => 5);
$arr4 = array("one" => "one"'3' => "three""0.5" => 5);

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

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

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

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

echo 
'<br><br><br>After 
 array_uintersect_assoc($arr1, $arr2, 
                                     $arr3, $arr4, 
                                     $data_compare_function):<br>'
;
var_dumparray_uintersect_assoc($arr1
                                                      
$arr2
                                                      
$arr3
                                                      
$arr4
                                                      
$data_compare_function) );

?>

  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;
    }
}
$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_assoc($a$b, array("cr""comp_func_cr"));
echo 
'<pre>';
var_dump($result);
echo 
'</pre>';

?>

  4 EXERCISE   

<?php

echo "Testing array_uintersect_assoc() : basic functionality - testing with multiple array arguments.";

include(
'compare_function.inc');
$data_compare_function 'compare_function';

// Initialise all required variables
$arr1 = array("one" => "one""02" => "two"'3' => "three""four""0.5" => 50.6 => 6"0x7" => "seven");
$arr2 = array("one" => "one""02" => "two"'3' => "three");
$arr3 = array("one" => "one"'3' => "three""0.5" => 5);
$arr4 = array("one" => "one"'3' => "three""0.5" => 5);

echo 
'<pre>';
var_dumparray_uintersect_assoc($arr1$arr2$arr3$arr4$data_compare_function) );
echo 
'</pre>';

?>

  5 EXERCISE   

<?php

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

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

include(
'compare_function.inc');
$data_compare_function '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_assoc($value$array2$data_compare_function) );
    } catch (
TypeError $e) {
        echo 
$e->getMessage(), "<br>";
    }
};

?>

  6 EXERCISE   

<?php

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

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

include(
'compare_function.inc');
$data_compare_function '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_assoc($array1$value$data_compare_function) );
    } catch (
TypeError $e) {
        echo 
$e->getMessage(), "<br>";
    }
};

?>

  7 EXERCISE   

<?php

echo "Testing array_uintersect_assoc() : usage variation - differing comparison functions.";

$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_assoc($arr1$arr2'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_assoc($arr1$arr2'too_many_parameters'));
} catch (
Throwable $e) {
    echo 
"Exception: " $e->getMessage() . "\n";
}

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

var_dump(array_uintersect_assoc($arr1$arr2'too_few_parameters'));

?>