array_diff_keyCOMPUTES the difference between ARRAYS using keys for comparison.
This function compares keys from $array1 against the keys from $array2 and returns the difference.
This function is like array_diff except the comparison is done on the keys instead of the values.
The two compared keys are considered equals only if are identical.
<?php
arr array_diff_key ( arr $array1 , arr $array2 [,
arr $arra3, ..., arr $arrayN ] )
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
?>
$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.
EXERCISE
<?php
$arr01a = array("a" => "airplane", "b" => "boat",
"s" => "ship", "t" => "train");
$arr01b = array("c" => "car", "b" => "boat",
"s" => "ship", "T" => "train" );
echo 'FIRST ARRAY $arr01a:<br>';
print_r($arr01a);
echo '<br><br>SECOND ARRAY $arr01b:<br>';
print_r($arr01b);
$arr01ab = array_diff_key($arr01a, $arr01b);
echo '<br><br><br>RESULTING ARRAY:<br>';
print_r($arr01ab);
?>
RESULT
FIRST ARRAY:
Array ( [a] => airplane [b] => boat [s] => ship [t] => train )
SECOND ARRAY:
Array ( [c] => car [b] => boat [s] => ship [T] => train )
RESULTING ARRAY
Array ( [a] => airplane [t] => train )
The comparison of the two ARRAYS was made considering, ONLY, their respective key.
Keys in uppercase and/or lowercase are considered different.
EXERCISE
<?php
$arr02a = array("a" => "airplane", "b" => "boat",
"s" => "ship", "t" => "train");
$arr02b = array("c" => "car", "b" => "boat",
"S" => "ship", "T" => "train" );
echo 'FIRST ARRAY $arr02a:<br>';
print_r($arr02a);
echo '<br><br>SECOND ARRAY $arr02b:<br>';
print_r($arr02b);
$arr02ab = array_diff_key($arr02a, $arr02b);
echo '<br><br><br>RESULTING ARRAY:<br>';
print_r($arr02ab);
?>
RESULT
FIRST ARRAY:
Array ( [a] => airplane [b] => boat [s] => ship [t] => train )
SECOND ARRAY:
Array ( [c] => car [b] => boat [S] => ship [T] => train )
RESULTING ARRAY
Array ( [a] => airplane [s] => ship [t] => train )
The comparison of the two ARRAYS was made considering, ONLY, their respective key.
Keys in uppercase and/or lowercase are considered different.
EXERCISE
<?php
$array1 = array("a" => "green", "b" => "brown",
"c" => "blue", "red", "");
$array2 = array("a" => "green", "yellow", "red", TRUE);
$array3 = array("red", "a"=>"brown", "");
echo 'The $array1:<br>';
var_dump($array1);
echo '<br><br>The $array2:<br>';
var_dump($array2);
echo '<br><br>The $array3:<br>';
var_dump($array3);
$result[] = array_diff_key($array1, $array2);
$result[] = array_diff_key($array1, $array3);
$result[] = array_diff_key($array2, $array3);
$result[] = array_diff_key($array1, $array2, $array3);
echo '<br><br>The $result:<br>';
var_dump($result);
?>
EXERCISE
<?php
/*
* Function is implemented in ext/standard/array.c
*/
$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_key($array1, $array2):<br>';
var_dump(array_diff_key($array1, $array2));
echo '<br><br>After array_diff_key($array2, $array1):<br>';
var_dump(array_diff_key($array2, $array1));
?>
EXERCISE
<?php
echo "Testing array_diff_key() : usage variation.<br><br>";
// Initialise function arguments not being substituted (if any)
$input_array = array(-07 => '-07', 0xA => '0xA');
$input_arrays = array(
'DECIMAL indexed' => array(10 => '10', '-17' => '-17'),
'OCTAL indexed' => array(-011 => '-011', 012 => '012'),
'HEXA indexed' => array(0x12 => '0x12', -0x7 => '-0x7', ),
);
echo 'The given $input_array:<br>';
print_r($input_array);
echo '<br><br>The given $input_arrays:<br>';
print_r($input_arrays);
// loop through each element of the array for arr1
foreach($input_arrays as $key =>$value) {
echo "<br><br><b>$key:</b><br>";
var_dump( array_diff_key($input_array, $value) );
echo '<br><br>';
var_dump( array_diff_key($value, $input_array) );
}
?>
EXERCISE
<?php
echo "Testing array_diff_key() : usage variation.<br><br>";
// Initialise function arguments
// not being substituted (if any)
$input_array = [0 => '0', 10 => '10', -10 => '-10',
20 =>'20', -20 => '-20'];
$float_indx_array = [0.0 => '0.0', 10.5 => '10.5',
-10.5 => '-10.5', 0.5 => '0.5'];
echo 'The given $input_array:<br>';
var_dump($input_array);
echo '<br><br>The given $float_indx_array:<br>';
var_dump($float_indx_array);
echo "<br><br>With float indexed array:<br>";
// loop through each element of the array for arr1
var_dump( array_diff_key($input_array, $float_indx_array) );
echo '<br><br>';
var_dump( array_diff_key($float_indx_array, $input_array) );
?>
EXERCISE
<?php
echo "Testing array_diff_key() : 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>';
var_dump($input_array);
echo '<br><br>The given $boolean_indx_array:<br>';
var_dump($boolean_indx_array);
echo "<br><br>With boolean indexed array:<br>";
// loop through each element of the array for arr1
var_dump( array_diff_key($input_array,
$boolean_indx_array) );
echo '<br><br>';
var_dump( array_diff_key($boolean_indx_array,
$input_array) );
?>
EXERCISE
<?php
echo "Testing array_diff_key() : usage variation.<br><br>";
// Initialise function arguments
// not being substituted (if any)
$input_array = 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>';
var_dump($input_array);
echo '<br><br>The given $input_arrays:<br>';
var_dump($input_arrays);
foreach($input_arrays as $key =>$value) {
echo "<br><br><b>$key;</b><br>";
// loop through each element of the array for arr1
var_dump( array_diff_key($input_array, $value) );
echo '<br><br>';
var_dump( array_diff_key($value, $input_array) );
}
?>
EXERCISE
<?php
echo "Testing array_diff_key() : 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>';
var_dump($array1);
echo '<br><br>The given $array2:<br>';
var_dump($array2);
echo "<br><br>With multi dimensional array;<br>";
var_dump( array_diff_key($array1, $array2) );
echo '<br><br>';
var_dump( array_diff_key($array2, $array1) );
?>