array_intersect
COMPUTES the intersection of ARRAYS.
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 values.
<?php
arr array_intersect ( arr $array1 , arr $array2 [,
arr $arra3, ..., arr $arrayN ] )
where,
$array1 = The first ARRAY to check
$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 - values to check.
$array2
The second ARRAY - to compare values against.
$array3
The third ARRAY - to compare values against.
$arrayN
The last ARRAY - to compare values against.
EXERCISE
<?php
$arr_current = [ "Lust", "Gluttony",
"Greed", "Sloth",
"Wrath", "Envy", "Pride"];
$arr_gregory = [ "Pride", "Envy",
"Anger", "Sadness",
"Avarice", "Gluttony", "Lust" ];
$arr_thomas = [ "Pride", "Greed",
"Gluttony", "Lust",
"Sloth", "Envy", "Anger" ];
$arr_common = array_intersect($arr_current,
$arr_gregory,
$arr_thomas);
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] => Gluttony [2] => Greed [3] => Sloth [4] => Wrath [5] => Envy [6] => Pride )
ACCORDING POPE GREGORY:
Array ( [0] => Pride [1] => Envy [2] => Anger [3] => Sadness [4] => Avarice [5] => Gluttony [6] => Lust )
ACCORDING ST. THOMAS AQUINAS:
Array ( [0] => Pride [1] => Greed [2] => Gluttony [3] => Lust [4] => Sloth [5] => Envy [6] => Anger )
COMMON AT ALL:
Array ( [0] => Lust [1] => Gluttony [5] => Envy [6] => Pride )
EXERCISE
<?php
$Acurrent = [ 'first' => "Lust", 'second' => "Gluttony",
'third' => "Greed", 'fourth' => "Sloth", 'fifth' => "Wrath",
'sixth' => "Envy", 'seventh' => "Pride"];
$Agregory = [ 'first' => "Pride", 'second' => "Envy",
'third' => "Anger", 'fourth' => "Sadness",
'fifth' => "Avarice", 'sixth' => "Gluttony",
'seventh' => "Lust" ];
$Athomas = [ 'first' => "Pride", 'second' => "Greed",
'third' => "Gluttony", 'fourth' => "Lust",
'fifth' => "Sloth", 'sixth' => "Envy",
'seventh' => "Anger" ];
$Acommon = array_intersect($Acurrent,
$Agregory,
$Athomas);
echo 'SEVEN DEADLY SINS.<br><br>CURRENT:<br>';
print_r($Acurrent);
echo '<br><br>ACCORDING POPE GREGORY:<br>';
print_r($Agregory);
echo '<br><br>ACCORDING ST. THOMAS AQUINAS:<br>';
print_r($Athomas);
echo '<br><br><br>COMMON AT ALL:<br>';
print_r($Acommon);
?>
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] => Pride [second] => Envy [third] => Anger [fourth] => Sadness [fifth] => Avarice [sixth] => Gluttony [seventh] => Lust )
ACCORDING ST. THOMAS AQUINAS:
Array ( [first] => Pride [second] => Greed [third] => Gluttony [fourth] => Lust [fifth] => Sloth [sixth] => Envy [seventh] => Anger )
COMMON AT ALL:
Array ( [first] => Lust [second] => Gluttony [sixth] => Envy [seventh] => Pride )
Parity between keys and values, of the first ARRAY, were kept.
EXERCISE
<?php
/*
* Testing the behavior of array_intersect()
* by passing array with
* binary values for $arr1 and $arr2 argument.
*/
echo "Testing array_intersect() : binary safe checking.<br><br>";
// array with binary values
$arr_binary = array(b"hello", b"world");
// simple array
$arr_normal = array("hello", "world");
echo 'The given $arr_binary:<br>';
var_dump($arr_binary);
echo '<br><br>The given $arr_normal:<br>';
var_dump($arr_normal);
// array with binary value for $arr1 argument
echo '<br><br>After array_intersect($arr_binary, $arr_normal):<br>';
var_dump( array_intersect($arr_binary, $arr_normal) );
// array with binary value for $arr2 argument
echo '<br><br>After array_intersect($arr_normal, $arr_binary):<br>';
var_dump( array_intersect($arr_normal, $arr_binary) );
// array with binary value for both $arr1 and $arr2 argument
echo '<br><br>After array_intersect($arr_binary, $arr_binary):<br>';
var_dump( array_intersect($arr_binary, $arr_binary) );
?>
EXERCISE
<?php
/*
* Testing the behavior of array_intersect()
* by passing 2-D arrays
* to both $arr1 and $arr2 argument.
* Optional argument takes the same
* value as that of $arr1
*/
echo "Testing array_intersect() : passing two dimensional
array to both \$arr1 and \$arr2 arguments.<br><br>";
// two dimensional arrays for $arr1 and $arr2 argument
$arr1 = array (
// arrays with default keys
array(1, 2, "hello", 'world'),
array(1, 2, 3, 4),
// arrays with explicit keys
array(1 => "one", 2 => "two", 3 => "three"),
array("ten" => 10, "twenty" => 20.00, "thirty" => 30)
);
$arr2 = array (
array(1, 2, 3, 4),
array(1 => "one", 2 => "two", 3 => "three")
);
echo 'The given $arr1:<br>';
var_dump($arr1);
echo '<br><br>The given $arr2:<br>';
var_dump($arr2);
/* Passing the entire array as argument to $arr1 and $arr2 */
// Calling array_intersect() with default arguments
echo "<br><br>Passing the entire
2-D array to \$arr1 and \$arr2<br>";
echo "With default arguments:<br>";
var_dump( array_intersect($arr1, $arr2) );
// Calling array_intersect() with more arguments
// additional argument passed is the same as $arr1
echo "<br><br>With more arguments:<br>";
var_dump( array_intersect($arr1, $arr2, $arr1) );
/* Passing the sub-array as argument to $arr1 and $arr2 */
// Calling array_intersect() with default arguments
echo "<br><br><br>Passing the sub-array to \$arr1 and \$arr2<br>";
echo "With default arguments:<br>";
var_dump( array_intersect($arr1[0], $arr2[0]) );
// Calling array_intersect() with more arguments
// additional argument passed is the same as $arr1
echo "<br><br>With more arguments:<br>";
var_dump( array_intersect($arr1[0], $arr2[0], $arr1[0]) );
?>
EXERCISE
<?php
/*
* Testing the functionality of array_intersect()
* by passing different
* associative arrays having different
* possible keys to $arr2 argument.
* The $arr1 argument is a fixed array.
*/
echo "Testing array_intersect() : assoc array
with different keys to \$arr2 argument.<br>";
// get an unset variable
$unset_var = 10;
unset ($unset_var);
// get a heredoc string
$heredoc = <<<EOT
Hello world
EOT;
// different variations of associative arrays
// to be passed to $arr2 argument
$arrays = array (
// empty array
/*1*/ array(),
// arrays with integer keys
array(0 => "0"),
array(1 => "1"),
array(1 => "1", 2 => "2", 3 => "3", 4 => "4"),
// arrays with float keys
/*5*/ array(2.3333 => "float"),
array(1.2 => "f1", 3.33 => "f2",
4.89999922839999 => "f3",
33333333.333333 => "f4"),
// arrays with string keys
/*7*/ array('\tHello' => 111, 're\td' => "color",
'\v\fworld' => 2.2, 'pen\n' => 33),
array("\tHello" => 111, "re\td" => "color",
"\v\fworld" => 2.2, "pen\n" => 33),
array("hello", $heredoc => "string"),
// heredoc
// array with unset variable
/*10*/ array( @$unset_var => "hello"),
// array with mixed keys
/*11*/ array('hello' => 1, "fruit" => 2.2,
133 => "int", 444.432 => "float",
@$unset_var => "unset", $heredoc => "heredoc")
);
// array to be passsed to $arr1 argument
$arr1 = array(1, "float", "f4", "hello",
2.2, 'color', "string", "pen\n", 11);
// loop through each sub-array within $arrays
// to check the behavior of array_intersect()
$iterator = 1;
foreach($arrays as $arr2) {
echo "<br><br>Iterator: $iterator<br>";
// Calling array_intersect() with default arguments
var_dump( array_intersect($arr1, $arr2) );
// Calling array_intersect() with more arguments.
// additional argument passed
// is the same as $arr1 argument
var_dump( array_intersect($arr1, $arr2, $arr1) );
$iterator++;
}
?>
EXERCISE
<?php
/*
* Passing different types of arrays
* to $arr2 argument and testing whether
* array_intersect() behaves in expected
* way with the other arguments passed to the function.
* The $arr1 argument is a fixed array.
*/
echo "Testing array_intersect() : Passing different
types of arrays to \$arr2 argument.<br><br>";
/* Different heredoc strings
passed as argument to $arr2 */
// heredoc with blank line
$blank_line = <<<EOT
EOT;
// heredoc with multiline string
$multiline_string = <<<EOT
hello world
The big brown fox jumped over;
the lazy dog
This is a double quoted string
EOT;
// heredoc with different whitespaces
$diff_whitespaces = <<<EOT
hello\r world\t
1111\t\t != 2222\v\v
heredoc\ndouble quoted string.
With\vdifferent\fwhite\vspaces
EOT;
// heredoc with quoted strings
// and numeric values
$numeric_string = <<<EOT
11 < 12. 123 >22
'single quoted string'
"double quoted string"
2222 != 1111.\t 0000 = 0000\n
EOT;
// array to be passsed to $arr1 argument
$arr1 = array (
1, 1.1, "hello", "one", NULL, 2,
'world', true, false, false => 5, 'aaaa\r', "aaaa\r",
$numeric_string, $diff_whitespaces,
"one" => "ten", 4 => "four", "two" => 2, 2 => "two",
'', null => "null", '' => 'emptys'
);
// arrays to be passed to $arr2 argument
$arrays = array (
/*1*/ array(1, 2),
// array with default keys and numeric values
array(1.1, 2.2),
// array with default keys & float values
array(false,true),
// array with default keys and boolean values
array(),
// empty array
/*5*/ array(NULL),
// array with NULL
array("a\v\f","aaaa\r","b","b\tbbb",
"c","\[\]\!\@\#\$\%\^\&\*\(\)\{\}"),
// array with double quoted strings
array('a\v\f','aaaa\r','b','b\tbbb',
'c','\[\]\!\@\#\$\%\^\&\*\(\)\{\}'),
// array with single quoted strings
array($blank_line, $multiline_string,
$diff_whitespaces, $numeric_string),
// array with heredocs
// associative arrays
/*9*/ array(1 => "one", 2 => "two", 3 => "three"),
// explicit numeric keys, string values
array("one" => 1, "two" => 2, "three" => 3 ),
// string keys & numeric values
array( 1 => 10, 2 => 20, 4 => 40, 3 => 30),
// explicit numeric keys and numeric values
array( "one" => "ten", "two" => "twenty", "three" => "thirty"),
// string key/value
array("one" => 1, 2 => "two", 4 => "four"),
//mixed
// associative array,
// containing null/empty/boolean values
// as key/value
/*14*/ array(NULL => "NULL", null => "null",
"NULL" => NULL,
"null" => null),
array(true => "true", false => "false",
"false" => false, "true" => true),
array("" => "emptyd", '' => 'emptys',
"emptyd" => "", 'emptys' => ''),
array(1 => '', 2 => "", 3 => NULL, 4 => null,
5 => false, 6 => true),
array('' => 1, "" => 2, NULL => 3, null => 4,
false => 5, true => 6),
// array with repetitive keys
/*19*/ array("One" => 1,
"two" => 2,
"One" => 10,
"two" => 20,
"three" => 3)
);
// loop through each sub-array within
// $arrays to check the behavior
// of array_intersect()
$iterator = 1;
foreach($arrays as $arr2) {
echo "<br><br>Iteration: $iterator<br>";
// Calling array_intersect() with default arguments
var_dump( array_intersect($arr1, $arr2) );
echo '<br><br>';
// Calling array_intersect() with more arguments
// additional argument passed is
// the same as $arr1 argument
var_dump( array_intersect($arr1, $arr2, $arr1) );
$iterator++;
}
?>
EXERCISE
<?php
/*
* Passing different types of arrays
* to $arr1 argument and testing whether
* array_intersect() behaves in expected
* way with the other arguments passed to the function
* The $arr2 argument is a fixed array.
*/
echo "Testing array_intersect() : Passing different types<br>
of arrays to \$arr1 argument.";
/* Different heredoc strings passed
as argument to $arr1 */
// heredoc with blank line
$blank_line = <<<EOT
EOT;
// heredoc with multiline string
$multiline_string = <<<EOT
hello world
The big brown fox jumped over;
the lazy dog
This is a double quoted string
EOT;
// heredoc with different whitespaces
$diff_whitespaces = <<<EOT
hello\r world\t
1111\t\t != 2222\v\v
heredoc\ndouble quoted string.
With\vdifferent\fwhite\vspaces
EOT;
// heredoc with quoted strings and numeric values
$numeric_string = <<<EOT
11 < 12. 123 >22
'single quoted string'
"double quoted string"
2222 != 1111.\t 0000 = 0000\n
EOT;
// arrays to be passed to $arr1 argument
$arrays = array (
/*1*/ array(1, 2),
// array with default keys and numeric values
array(1.1, 2.2),
// array with default keys & float values
array(false,true),
// array with default keys and boolean values
array(),
// empty array
/*5*/ array(NULL),
// array with NULL
array("a\v\f","aaaa\r","b","b\tbbb",
"c","\[\]\!\@\#\$\%\^\&\*\(\)\{\}"),
// array with double quoted strings
array('a\v\f','aaaa\r','b','b\tbbb',
'c','\[\]\!\@\#\$\%\^\&\*\(\)\{\}'),
// array with single quoted strings
array($blank_line, $multiline_string,
$diff_whitespaces, $numeric_string),
// array with heredocs
// associative arrays
/*9*/ array(1 => "one", 2 => "two", 3 => "three"),
// explicit numeric keys, string values
array("one" => 1, "two" => 2, "three" => 3 ),
// string keys & numeric values
array( 1 => 10, 2 => 20, 4 => 40, 3 => 30),
// explicit numeric keys and numeric values
array( "one" => "ten", "two" => "twenty",
"three" => "thirty"),
// string key/value
array("one" => 1, 2 => "two", 4 => "four"),
//mixed
// associative array,
// containing null/empty/boolean values
// as key/value
/*14*/ array(NULL => "NULL", null => "null",
"NULL" => NULL, "null" => null),
array(true => "true", false => "false",
"false" => false, "true" => true),
array("" => "emptyd", '' => 'emptys',
"emptyd" => "", 'emptys' => ''),
array(1 => '', 2 => "", 3 => NULL,
4 => null, 5 => false, 6 => true),
array('' => 1, "" => 2, NULL => 3,
null => 4, false => 5, true => 6),
// array with repetitive keys
/*19*/ array("One" => 1, "two" => 2, "One" => 10,
"two" => 20, "three" => 3)
);
// array to be passsed to $arr2 argument
$arr2 = array (
1, 1.1, "hello", "one", NULL, 2,
'world', true, false, false => 5, 'aaaa\r', "aaaa\r",
$numeric_string, $diff_whitespaces,
"one" => "ten", 4 => "four", "two" => 2, 2 => "two",
'', null => "null", '' => 'emptys'
);
// loop through each sub-array within $arrays
// to check the behavior of array_intersect()
$iterator = 1;
foreach($arrays as $arr1) {
echo "<br><br>Iterator: $iterator<br>";
// Calling array_intersect() with default arguments
var_dump( array_intersect($arr1, $arr2) );
// Calling array_intersect() with more arguments.
// additional argument passed is
// the same as $arr1 argument
var_dump( array_intersect($arr1, $arr2, $arr1) );
$iterator++;
}
?>