array_udiff_uassocCOMPUTES the difference of ARRAYS with additional index check, compares data and indexes by a callback function.
This function returns an ARRAY containing all the entries from $array1 that are not present in any of the other arrays.
Note that the keys are used in the comparison unlike array_diff and array_udiff.
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.
The $key_compare_func uses behaviour unlike what array_diff_assoc does, since the latter compares the indices by using an internal function
<?php
arr array_udiff_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 comparisson values function
$key_compare_func = The comparisson kyes function
?>
$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.
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;
}
static function comp_func_key($a, $b) {
if ($a === $b) return 0;
return ($a > $b) ? 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_udiff_uassoc($a, $b, array("cr", "comp_func_cr"), array("cr", "comp_func_key"));
echo '<pre>';
var_dump($result);
echo '</pre>';
?>
EXERCISE
<?php
echo "Testing array_udiff_uassoc() : usage variation.";
// Initialise function arguments
// not being substituted (if any)
$array2 = array(1, 2);
include('compare_function.inc');
$data_comp_func = 'compare_function';
$key_comp_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 (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_udiff_uassoc($value, $array2, $data_comp_func, $key_comp_func) );
} catch (TypeError $e) {
echo $e->getMessage(), "<br>";
}
};
?>
EXERCISE
<?php
echo "Testing array_udiff_uassoc() : usage variation.";
// Initialise function arguments
// not being substituted (if any)
$array1 = array(1, 2);
include('compare_function.inc');
$data_comp_func = 'compare_function';
$key_comp_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 (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_udiff_uassoc($array1, $value, $data_comp_func, $key_comp_func) );
} catch (TypeError $e) {
echo $e->getMessage(), "<br>";
}
};
?>
EXERCISE
<?php
echo "Testing array_udiff_uassoc() :
usage variation -
differing comparison functions.";
$arr1 = array(1);
$arr2 = array(1);
echo "<br><br>Comparison function with an incorrect return value:<br>";
function incorrect_return_value ($val1, $val2) {
return array(1);
}
var_dump(array_udiff_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_udiff_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_udiff_uassoc($arr1, $arr2, 'too_few_parameters', 'too_few_parameters'));
?>
EXERCISE
<?php
/* Prototype : array array_udiff_uassoc(array arr1, array arr2 [, array ...], callback data_comp_func, callback key_comp_func)
* Description: Returns the entries of arr1 that have values which are not present in any of the others arguments but do additional checks whether the keys are equal. Keys and elements are compared by user supplied functions.
* Source code: ext/standard/array.c
* Alias to functions:
*/
echo "Testing array_udiff_uassoc() : usage variation.";
// Initialise function arguments not being substituted (if any)
$arr1 = array(1, 2);
$arr2 = array(1, 2);
include('compare_function.inc');
$data_comp_func = 'compare_function';
$key_comp_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 (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 ...
foreach($inputs as $key =>$value) {
echo "<br><br>$key:<br>";
var_dump( array_udiff_uassoc($arr1, $arr2, $value, $data_comp_func, $key_comp_func) );
};
?>