array_intersect_keyCOMPUTES the intersection of ARRAYS using keys for comparison.
This function returns an ARRAY containing all the entries from $array1 that are present in any of the other arrays.
Multiple occurrences in $array1 are all treated the same way.
In order for two elements to be considered identical, they must have the same keys.
<?php
arr array_intersect_key ( arr $array1 , arr $array2 [,
arr $arra3, ..., arr $arrayN ] )
where,
$array1 = The first ARRAY to keys check
$array2 = The second ARRAY to compare keys against
$array3 = The third ARRAY to compare keys against
. . . . . . . . . . . . . . .
$arrayN = The last ARRAY to compare keys against
?>
$array1
The first ARRAY - keys to check.
$array2
The second ARRAY - to compare keys against.
$array3
The third ARRAY - to compare keys against.
$arrayN
The last ARRAY - to compare keys against.
EXERCISE
<?php
$az1 = [ "wa" => "Water",
"co" => "Cobalt",
"cy" => "Cyan",
"he" => "Heavenly",
"ca" => "Cadet" ];
$az2 = [ "ca" => "Cadet",
"ma" => "Marine",
"sl" => "Slate",
"pe" => "Petroleum",
"cy" => "Cyan" ];
$az3 = [ "ma" => "Marine",
"ca" => "Cadet",
"re" => "Real",
"cy" => "Cyan",
"na" => "Naval" ];
echo 'FIRST ARRAY:<br>';
print_r($az1);
echo '<br><br>SECOND ARRAY:<br>';
print_r($az2);
echo '<br><br>THIRD ARRAY:<br>';
print_r($az3);
echo '<br><br><br>RESULTING ARRAY:<br>';
$az123 = array_intersect_key($az1, $az2, $az3);
print_r($az123);
?>
RESULT
FIRST ARRAY:
Array ( [wa] => Water [co] => Cobalt [cy] => Cyan [he] => Heavenly [ca] => Cadet )
SECOND ARRAY:
Array ( [ca] => Cadet [ma] => Marine [sl] => Slate [pe] => Petroleum [cy] => Cyan )
THIRD ARRAY:
Array ( [ma] => Marine [ca] => Cadet [re] => Real [cy] => Cyan [na] => Naval )
RESULTING ARRAY:
Array ( [cy] => Cyan [ca] => Cadet )
All keys are the same in all ARRAYS worked by this function.
All values also belong to the first ARRAY.
EXERCISE
<?php
$tean1 = [ "CORINTHIANS", "PALMEIRAS",
"SANTOS", "SÃO PAULO", "GRÊMIO" ];
$tean2 = [ "VASCO", "CRUZEIRO", "FLUMININSE", "SANTOS" ];
$tean3 = [ "VASCO", "GRÊMIO", "VITÓRIA", "SANTOS", "BAHIA" ];
echo 'FIRST ARRAY:<br>';
print_r($tean1);
echo '<br><br>SECOND ARRAY:<br>';
print_r($tean2);
echo '<br><br>THIRD ARRAY:<br>';
print_r($tean3);
echo '<br><br><br>RESULTING ARRAY:<br>';
$tean123 = array_intersect_key($tean1, $tean2, $tean3);
print_r($tean123);
?>
RESULT
FIRST ARRAY:
Array ( [0] => CORINTHIANS [1] => PALMEIRAS [2] => SANTOS [3] => SÃO PAULO [4] => GRÊMIO )
SECOND ARRAY:
Array ( [0] => VASCO [1] => CRUZEIRO [2] => FLUMININSE [3] => SANTOS )
THIRD ARRAY:
Array ( [0] => VASCO [1] => GRÊMIO [2] => VITÓRIA [3] => SANTOS [4] => BAHIA )
RESULTING ARRAY:
Array ( [0] => CORINTHIANS [1] => PALMEIRAS [2] => SANTOS [3] => SÃO PAULO )
All keys are the same in all ARRAYS worked by this function.
All values also belong to the first ARRAY.
EXERCISE
<?php
$color1 = [ "Yellow", "Green", "Red", "Blue", "Rose" ];
$color2 = [ 'cy' => "Cyan", 'ma' => "Magenta", "Blue" ];
$color3 = [ 'cy' => "Cyan", 'ro' => "Rose", "Blue" ];
echo 'FIRST ARRAY:<br>';
print_r($color1);
echo '<br><br>SECOND ARRAY:<br>';
print_r($color2);
echo '<br><br>THIRD ARRAY:<br>';
print_r($color3);
echo '<br><br><br>RESULTING ARRAY:<br>';
$color123 = array_intersect_key($color1, $color2, $color3);
print_r($color123);
?>
RESULT
FIRST ARRAY:
Array ( [0] => Yellow [1] => Green [2] => Red [3] => Blue [4] => Rose )
SECOND ARRAY:
Array ( [cy] => Cyan [ma] => Magenta [0] => Blue )
THIRD ARRAY:
Array ( [cy] => Cyan [ro] => Rose [0] => Blue )
RESULTING ARRAY:
Array ( [0] => Yellow )
The unique value that displays the same index key in the first ARRAY is 'Yellow' whose key is ZERO.
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><br>
After array_intersect_key($array1, $array2):<br>';
var_dump(array_intersect_key($array1, $array2));
?>
EXERCISE
<?php
echo "Testing array_intersect_key() : usage variation.";
// Initialise function arguments
// not being substituted (if any)
$input_array = [ 0 => '0', -1 => '-1' , 02 => 'two',
-07 => '-07', 0xA => '0xA',
-0xC => '-0xc' ];
$input_arrays = [
'decimal indexed' => [ 10 => '10', '-17' => '-17' ],
'octal indexed' => [ -011 => '-011', 012 => '012' ],
'hexa indexed' => [0x12 => '0x12', -0x7 => '-0x7', ],
];
echo '<br><br>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>";
echo '<br>
After array_intersect_key($input_array, $value):<br>';
var_dump( array_intersect_key($input_array, $value) );
echo '<br><br>
After array_intersect_key($value,$input_array):<br>';
var_dump( array_intersect_key($value,$input_array) );
}
?>
EXERCISE
<?php
echo "Testing array_intersect_key() : usage variation.";
// Initialise function arguments not being substituted (if any)
$input_array = array(0 => '0',
10 => '10',
-10 => '-10');
$float_indx_array = array(0.0 => '0.0',
10.5 => '10.5',
-10.5 => '-10.5',
0.5 => '0.5');
echo '<br><br>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>";
echo 'After
array_intersect_key($input_array, $float_indx_array):<br>';
var_dump( array_intersect_key($input_array,
$float_indx_array) );
echo '<br><br>After
array_intersect_key($float_indx_array, $input_array):<br>';
var_dump( array_intersect_key($float_indx_array,
$input_array));
?>
EXERCISE
<?php
echo "Testing array_intersect_key() : usage variation.";
// Initialise function arguments not being substituted (if any)
$input_array = array(0 => '0',
1 => '1' ,
-10 => '-10');
$boolean_indx_array = array(true => 'boolt', false => 'boolf',
TRUE => 'boolT', FALSE => 'boolF');
echo '<br><br>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>";
echo 'After
array_intersect_key($input_array, $boolean_indx_array):<br>';
var_dump( array_intersect_key($input_array,
$boolean_indx_array) );
echo '<br><br>After
array_intersect_key($boolean_indx_array, $input_array):<br>';
var_dump( array_intersect_key($boolean_indx_array,
$input_array) );
?>
EXERCISE
<?php
echo "Testing array_intersect_key() : usage variation.";
// Initialise function arguments not being substituted (if any)
$input_array = array(0 => '0', 1 => '1' , -10 => '-10' , null => 'null');
//get an unset variable
$unset_var = 10;
unset ($unset_var);
$input_arrays = array(
'null indexed' => array(NULL => 'null 1', null => 'null 2'),
'undefined indexed' => array(@$undefined_var => 'undefined'),
'unset indexed' => array(@$unset_var => 'unset'),
);
echo '<br><br>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>";
echo 'After array_intersect_key($input_array, $value):<br>';
var_dump( array_intersect_key($input_array, $value) );
echo '<br><br>
After array_intersect_key($value,$input_array):<br>';
var_dump( array_intersect_key($value,$input_array) );
}
?>
EXERCISE
<?php
echo "Testing array_intersect_key() : usage variation.";
/// 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 '<br><br>The given $array1:<br>';
var_dump($array1);
echo '<br><br>The given $array2:<br>';
var_dump($array2);
echo '<br><br><br>After array_intersect_key($array1, $array2):<br>';
var_dump( array_intersect_key($array1, $array2) );
echo '<br><br><br>After array_intersect_key($array2, $array1):<br>';
var_dump( array_intersect_key($array2, $array1 ) );
?>