array_uintersectCOMPUTES the intersection of ARRAYS, compares data and indexes by a callback function.
This function returns an ARRAY containing all the values of $array1 that are present in any of the other arrays.
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 ( arr $array1 , arr $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.
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_uintersect($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 [2] => Gluttony [6] => Envy )
Elements are part of all ARRAYS.
The keys belongs to the FIRST ARRAY.
The strcmp function was used as callback which is case-sensitive.
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($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>';
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 [second] => Gluttony [sixth] => Envy [seventh] => Pride )
Elements are part of all ARRAYS.
The keys belongs to the FIRST ARRAY.
The strcmp function was used as callback which is case-sensitive.
EXERCISE
<?php
$Ccurr = [ 'first' => "Lust", 'second' => "Gluttony",
'third' => "Greed", 'fourth' => "Sloth", 'fifth' => "Wrath",
'sixth' => "Envy", 'seventh' => "Pride"];
$Cgreg = [ 'first' => "LUST", 'second' => "ENVY",
'third' => "Anger", 'fourth' => "Sadness",
'fifth' => "Avarice", 'sixth' => "Gluttony",
'seventh' => "Pride" ];
$Cthom = [ 'first' => "Lust", 'second' => "Greed",
'third' => "GLUTTONY", 'fourth' => "Pride",
'fifth' => "Sloth", 'sixth' => "Envy",
'seventh' => "Anger" ];
$Ccomm = array_uintersect($Ccurr,
$Cgreg,
$Cthom,
'strnatcasecmp');
echo 'SEVEN DEADLY SINS.<br><br>CURRENT:<br>';
print_r($Ccurr);
echo '<br><br>ACCORDING POPE GREGORY:<br>';
print_r($Cgreg);
echo '<br><br>ACCORDING ST. THOMAS AQUINAS:<br>';
print_r($Cthom);
echo '<br><br><br>SEVEN DEADLY SINS - COMMONS<br><br>';
print_r($Ccomm);
?>
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 : -1;
}
}
$a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1 => new cr(4), 2 => new cr(-15),);
$b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1 => new cr(4), 2 => new cr(-15),);
$result = array_uintersect($a, $b, array("cr", "comp_func_cr"));
echo '<pre>';
var_dump($result);
echo '</pre>';
?>
EXERCISE
<?php
echo "Testing array_uintersect() : usage variation.";
// Initialise function arguments
// not being substituted (if any)
$array2 = array(1, 2);
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 (1, 2, 3);
$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_dump( array_uintersect($value, $array2, $data_compare_function) );
} catch (TypeError $e) {
echo $e->getMessage(), "<br>";
}
};
?>
EXERCISE
<?php
echo "Testing array_uintersect() : usage variation.";
// Initialise function arguments
// not being substituted (if any)
$array1 = array(1, 2);
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 (1, 2, 3);
$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_dump( array_uintersect($array1, $value, $data_compare_function) );
} catch (TypeError $e) {
echo $e->getMessage(), "<br>";
}
};
?>
EXERCISE
<?php
echo "Testing array_uintersect() : 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($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($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($arr1, $arr2, 'too_few_parameters'));
?>