array_diff_ukeyCOMPUTES the difference of ARRAYS using a callback function on the keys for comparison.
This function returns an ARRAY containing all the entries from $array1 that are not present in any of the other arrays.
This function is like array_diff except the comparison is done on the keys instead of the values.
The $key_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.
A CALLBACK occurs when the call originator is immediately called back in a second response and when the call originator is immediately called back in a second response.
The call back process can be repeated several times.
Prior to PHP 7.0.0 this value is an INTEGER in the range -2147483648 to 2147483647.
<?php
arr array_diff_ukey ( arr $array1 , arr $array2 [,
arr $arra3, ..., arr $arrayN ],
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
$key_compare_func = The comparisson 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.
$key_compare_func
The callback comparison function.
EXERCISE
<?php
function ukcompare ($k1, $k2)
{
if ($k1 === $k2)
{
return false;
}
elseif($k1 !== $k2)
{
return true;
}
else
{
return NULL;
}
}
$arruk01a = [ "s" => 'string', "a" => 'array',
"n" => 'number', "o" => 'object',
"R" => 'resource' ];
$arruk01b = [ "S" => 'string',
"a" => 'ARRAY',
"r" => 'resource' ];
echo 'The given $arruk01a:<br>';
var_dump($arruk01a);
echo '<br><br>The given $$arruk01b:<br>';
var_dump($arruk01b);
$arruk01ab = array_diff_ukey ($arruk01a,
$arruk01b,
'ukcompare');
echo '<br><br>The resulting $arruk01ab:<br>';
print_r($arruk01ab);
?>
RESULT
The given $arruk01a:
array(5) { ["s"]=> string(6) "string" ["a"]=> string(5) "array" ["n"]=> string(6) "number" ["o"]=> string(6) "object" ["R"]=> string(8) "resource" }
The given $$arruk01b:
array(3) { ["S"]=> string(6) "string" ["a"]=> string(5) "ARRAY" ["r"]=> string(8) "resource" }
The resulting $arruk01ab:
Array ( [s] => string [n] => number [o] => object [R] => resource )
EXERCISE
<?php
$a = array(1, 6, 2, -20, 15, 1200, -2500);
$b = array(0, 7, 2, -20, 11, 1100, -2500);
$c = array(0, 6, 2, -20, 19, 1000, -2500);
$d = array(3, 8,-2, -20, 14, 900, -2600);
echo 'The given $a:<br>';
var_dump($a);
echo '<br><br>The given $b:<br>';
var_dump($b);
echo '<br><br>The given $c:<br>';
var_dump($c);
echo '<br><br>The given $d:<br>';
var_dump($d);
$a_f = array_flip($a);
$b_f = array_flip($b);
$c_f = array_flip($c);
$d_f = array_flip($d);
$i = 1;
echo '<br><br><br>After array_flip($a);:<br>';
var_dump($a_f);
echo '<br><br>After array_flip($b);:<br>';
var_dump($b_f);
echo '<br><br>After array_flip($c);:<br>';
var_dump($c_f);
echo '<br><br>After array_flip($d);:<br>';
var_dump($d_f);
/* give nicer values */
foreach ($a_f as $k=> &$a_f_el) { $a_f_el =$k*2;}
foreach ($b_f as $k=> &$b_f_el) { $b_f_el =$k*2;}
foreach ($c_f as $k=> &$c_f_el) { $c_f_el =$k*2;}
foreach ($d_f as $k=> &$d_f_el) { $d_f_el =$k*2;}
echo "<br><br><br>------ Test $i --------<br>";$i++;
// 1
var_dump(array_diff_key($a_f, $b_f));
// keys -> 1, 6, 15, 1200
echo '<br><br>';
var_dump(array_diff_ukey($a_f, $b_f, "comp_func"));
// 1, 6, 15, 1200
echo "<br><br><br>------ Test $i --------<br>";$i++;
// 2
var_dump(array_diff_key($a_f, $c_f));
// keys -> 1, 15, 1200
echo '<br><br>';
var_dump(array_diff_ukey($a_f, $c_f, "comp_func"));
// 1, 15, 1200
echo "<br><br><br>------ Test $i --------<br>";$i++;
// 3
var_dump(array_diff_key($a_f, $d_f));
// 1, 6, 2, 15, 1200, -2500
echo '<br><br>';
var_dump(array_diff_ukey($a_f, $d_f, "comp_func"));
// 1, 6, 2, 15, 1200, -2500
echo "<br><br><br>------ Test $i --------<br>";$i++;
// 4
var_dump(array_diff_key($a_f, $b_f, $c_f));
// 1, 15, 1200
echo '<br><br>';
var_dump(array_diff_ukey($a_f, $b_f, $c_f, "comp_func"));
// 1, 15, 1200
echo "<br><br><br>------ Test $i --------<br>";$i++;
// 5
var_dump(array_diff_key($a_f, $b_f, $d_f));
// 1, 6, 15, 1200
echo '<br><br>';
var_dump(array_diff_ukey($a_f, $b_f, $d_f, "comp_func"));
// 1, 6, 15, 1200
echo "<br><br><br>------ Test $i --------<br>";$i++;
// 6
var_dump(array_diff_key($a_f, $b_f, $c_f, $d_f));
// 1, 15, 1200
echo '<br><br>';
var_dump(array_diff_ukey($a_f, $b_f, $c_f, $d_f, "comp_func"));
//1, 15, 1200
echo "<br><br><br>------ Test $i --------<br>";$i++;
// 7
var_dump(array_diff_key($b_f, $c_f));
// 7, 11, 1100
echo '<br><br>';
var_dump(array_diff_ukey($b_f, $c_f, "comp_func"));
//7, 11, 1100
echo "<br><br><br>------ Test $i --------<br>";$i++;
// 8
var_dump(array_diff_key($b_f, $d_f));
//0, 7, 2, 11, 1100, -2500
echo '<br><br>';
var_dump(array_diff_ukey($b_f, $d_f, "comp_func"));
//0, 7, 2, 11, 1100, -2500
echo "<br><br><br>------ Test $i --------<br>";$i++;
// 9
var_dump(array_diff_key($b_f, $c_f, $d_f));
// 7, 11, 1100
echo '<br><br>';
var_dump(array_diff_ukey($b_f, $c_f, $d_f, "comp_func"));
// 7, 11, 1000
function comp_func($a, $b) {
if ($a === $b) return 0;
return ($a > $b)? 1:-1;
}
?>
EXERCISE
<?php
/*
* Function is implemented in ext/standard/array.c
*/
function key_compare_func($key1, $key2) {
if ($key1 == $key2) return 0;
else if ($key1 > $key2) return 1;
else return -1;
}
$array1 = array('blue' => 1, 'red' => 2,
'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6,
'yellow' => 7, 'cyan' => 8);
echo 'The given $array1:<br>';
var_dump($array1);
echo '<br><br>The given $array2:<br>';
var_dump($array2);
echo '<br><br>After array_diff_ukey($array1,
$array2,
\'key_compare_func\'):<br>';
var_dump(array_diff_ukey($array1,
$array2,
'key_compare_func'));
?>
EXERCISE
<?php
echo "Testing array_diff_ukey() : usage variation.<br><br>";
// Initialise function arguments
// not being substituted (if any)
$array1 = array(
'first' => array('blue' => 1, 'red' => 2),
'second' => array('yellow' => 7),
'third' => array(0 => 'zero'),
);
$array2 = array (
'first' => array('blue' => 1, 'red' => 2,),
'second' => array('cyan' => 8),
'fourth' => array(2 => 'two'),
);
echo 'The given $array1:<br><pre>';
var_dump($array1);
echo '</pre><br><br>The given $array2:<br><pre>';
var_dump($array2);
echo '</pre><br>';
echo "With multi dimensional array.<br><br>";
echo 'After array_diff_ukey($array1, $array2, \'strcasecmp\'):<br><pre>';
var_dump( array_diff_ukey($array1, $array2, 'strcasecmp') );
echo '</pre><br>After array_diff_ukey($array2, $array1, \'strcasecmp\'):<br><pre>';
var_dump( array_diff_ukey($array2, $array1, 'strcasecmp') );
echo '</pre>';
?>
EXERCISE
<?php
echo "Testing array_diff_ukey() : usage variation.<br><br>";
// Initialise function arguments
// not being substituted (if any)
$input_array = [ -07 => '-07', 0xA => '0xA' ];
$input_arrays = [
'DECIMAL indexed' => [ 10 => '10', '-17' => '-17' ],
'OCTAL indexed' => [ -011 => '-011', 012 => '012' ],
'HEXA indexed' => [ 0x12 => '0x12', -0x7 => '-0x7', ],
];
echo 'The given $input_array:<br><pre>';
var_dump($input_array);
echo '</pre><br><br>The given $input_arrays:<br><pre>';
var_dump($input_arrays);
echo '</pre><br>';
function key_compare_func($key1, $key2)
{
return strcasecmp($key1, $key2);
}
foreach($input_arrays as $key => $value) {
echo "<u>$key:</u><br>";
echo 'After array_diff_ukey($value,
$input_array, \'key_compare_func\'):<br><pre>';
var_dump( array_diff_ukey($value,
$input_array,
'key_compare_func') );
echo '</pre>After array_diff_ukey($input_array,
$value, \'key_compare_func\'):<br><pre>';
var_dump( array_diff_ukey($input_array,
$value,
'key_compare_func') );
echo '</pre>';
}
?>
EXERCISE
<?php
echo "Testing array_diff_ukey() : usage variation.<br><br>";
// Initialise function arguments
// not being substituted (if any)
$input_array = [0 => '0', 10 => '10', -10 => '-10', 20 =>'20'];
$float_indx_array = [0.0 => '0.0', 10.5 => '10.5', -30.5 => '-30.5'];
echo 'The given $input_array:<br><pre>';
var_dump($input_array);
echo '</pre><br><br>The given $float_indx_array:<br><pre>';
var_dump($float_indx_array);
echo '</pre><br>';
function key_compare_func($key1, $key2)
{
return strcasecmp($key1, $key2);
}
echo "With float indexed array.<br><br>";
echo 'After array_diff_ukey($float_indx_array,
$input_array, \'key_compare_func\'):<br><pre>';
var_dump( array_diff_ukey($float_indx_array,
$input_array,
'key_compare_func') );
echo '</pre>After array_diff_ukey($input_array,
$float_indx_array, \'key_compare_func\'):<br><pre>';
var_dump( array_diff_ukey($input_array,
$float_indx_array,
'key_compare_func') );
echo '</pre>';
?>
EXERCISE
<?php
echo "Testing array_diff_ukey() : usage variation.<br><br>";
// Initialise function arguments
// not being substituted (if any)
$input_array = array(0 => '0', 1 => '1', -10 => '-10',
'true' => 1, 'false' => 0);
$boolean_indx_array = array(true => 'boolt', false => 'boolf',
TRUE => 'boolT', FALSE => 'boolF');
echo 'The given $input_array:<br><pre>';
var_dump($input_array);
echo '</pre><br><br>The given $boolean_indx_array:<br><pre>';
var_dump($boolean_indx_array);
echo '</pre><br>';
function key_compare_func($key1, $key2)
{
return strcasecmp($key1, $key2);
}
echo "With boolean indexed array.<br><br>";
echo 'After array_diff_ukey($boolean_indx_array,
$input_array, \'key_compare_func\'):<br><pre>';
var_dump( array_diff_ukey($boolean_indx_array,
$input_array,
'key_compare_func') );
echo '</pre>After array_diff_ukey($input_array,
$boolean_indx_array, \'key_compare_func\'):<br><pre>';
var_dump( array_diff_ukey($input_array,
$boolean_indx_array,
'key_compare_func') );
echo '</pre>';
?>
EXERCISE
<?php
echo "Testing array_diff_ukey() : usage variation.<br><br>";
// Initialise function arguments not being substituted (if any)
$input_array = [10 => '10', "" => 'empty'];
//get an unset variable
$unset_var = 10;
unset ($unset_var);
$input_arrays = [
'NULL indexed' => [NULL => 'null 1', null => 'null 2'],
'UNDEFINED indexed' => [@$undefined_var => 'undefined'],
'UNSET indexed' => [@$unset_var => 'unset'],
];
echo 'The given $input_array:<br><pre>';
var_dump($input_array);
echo '</pre><br><br>The given $input_arrays:<br><pre>';
var_dump($input_arrays);
echo '</pre><br>';
foreach($input_arrays as $key =>$value) {
echo "<br><br><b>$key:</b><br>";
echo 'After array_diff_ukey($value,
$input_array, \'strcasecmp\'):<br><pre>';
var_dump( array_diff_ukey($value,
$input_array,
'strcasecmp') );
echo '</pre>After array_diff_ukey($input_array,
$value, \'strcasecmp\'):<br><pre>';
var_dump( array_diff_ukey($input_array,
$value,
'strcasecmp') );
echo '</pre>';
}
?>