array_diffCOMPUTES the difference between ARRAYS.
This function returns an ARRAY containing all the entries from $array1 that are not 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 value.
This function only checks one dimension of a n-dimensional ARRAY.
Deeper dimensions can be made using this function to compare identical keys.
<?php
arr array_diff ( 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
// Brazilian football, (soccer), teams
$arr01a = [ "Corinthians", "Santos",
"Palmeiras", "São Paulo", "Portuguêsa" ];
$arr01b = [ "Palmeiras", "São Paulo", "Portuguêsa" ];
$arr01c = [ "Vasco", "Botafogo", "Flamengo" ];
echo 'First ARRAY:<br>';
print_r($arr01a);
echo '<br><br>Second ARRAY:<br>';
print_r($arr01b);
echo '<br><br>Third ARRAY:<br>';
print_r($arr01c);
$arr01abc = array_diff($arr01a, $arr01b, $arr01c);
echo '<br><br>After array_diff:<br>';
print_r($arr01abc);
?>
RESULT
First ARRAY:
Array ( [0] => Corinthians [1] => Santos [2] => Palmeiras [3] => São Paulo [4] => Portuguêsa )
Second ARRAY:
Array ( [0] => Palmeiras [1] => São Paulo [2] => Portuguêsa )
Third ARRAY:
Array ( [0] => Vasco [1] => Botafogo [2] => Flamengo )
After array_diff:
Array ( [0] => Corinthians [1] => Santos )
Note that these teams are only present in the first array and are absent in the others.
EXERCISE
<?php
// Brazilian football, (soccer), teams
$arr02a = [ "Corinthians", "Santos",
"Palmeiras", "São Paulo", "Portuguêsa" ];
$arr02b = [ "CORINTHIANS", "Palmeiras",
"São Paulo", "Portuguêsa" ];
$arr02c = [ "Vasco", "Botafogo", "Flamengo" ];
print_r($arr02a);
echo '<br><br>';
print_r($arr02b);
echo '<br><br>';
print_r($arr02c);
echo '<br><br>';
$arr02abc = array_diff($arr02a, $arr02b, $arr02c);
print_r($arr02abc);
?>
RESULT
First ARRAY:
Array ( [0] => Corinthians [1] => Santos [2] => Palmeiras [3] => São Paulo [4] => Portuguêsa )
Second ARRAY:
Array ( [0] => CORINTHIANS [1] => Palmeiras [2] => São Paulo [3] => Portuguêsa )
Third ARRAY:
Array ( [0] => Vasco [1] => Botafogo [2] => Flamengo )
After array_diff:
Array ( [0] => Corinthians [1] => Santos )
Note that these teams are only present in the first array and are absent in the others.
Note that the elements must correspond, exactly, to the array to compare from.
EXERCISE
<?php
// Brazilian football, (soccer), teams
$arr03a = [ "Santos", "Corinthians",
[ "Palmeiras", "São Paulo", "Portuguêsa" ] ];
$arr03b = [ "Palmeiras", [ "São Paulo", "Portuguêsa" ] ];
$arr03c = [ "Santos", [ "Vasco", "Botafogo", "Flamengo" ] ];
print_r($arr03a);
echo '<br><br>';
print_r($arr03b);
echo '<br><br>';
print_r($arr03c);
echo '<br><br>';
$arr03abc = array_diff($arr03a, $arr03b, $arr03c);
print_r($arr03abc);
?>
EXERCISE
<?php
/*
* Test basic functionality of array_diff
*/
echo "Testing basic functionality:<br><br>";
//Test indexed array with integers as elements
$array_int1 = array (1, 2, 3, 4);
$array_int2 = array (3, 4, 5, 6);
echo 'The given int ARRAY(1):<br>';
var_dump($array_int1);
echo '<br><br>The given int ARRAY(2):<br>';
var_dump($array_int2);
echo "<br><br>Test indexed array with integers as elements:<br>";
var_dump(array_diff($array_int1, $array_int2));
echo '<br><br>';
var_dump(array_diff($array_int2, $array_int1));
//Test indexed array with strings as elements
$array_string1 = array ('one', 'two', 'three', 'four');
$array_string2 = array ('three', 'four', 'five', 'six');
echo '<br><br>The given str ARRAY(1):<br>';
var_dump($array_string1);
echo '<br><br>The given str ARRAY(2):<br>';
var_dump($array_string2);
echo "<br><br>Test indexed array
with strings as elements:<br>";
var_dump(array_diff($array_string1, $array_string2));
echo '<br><br>';
var_dump(array_diff($array_string2, $array_string1));
//Test associative array with strings
//as keys and integers as elements
$array_assoc_int1 = array ('one' => 1, 'two' => 2,
'three' => 3, 'four' => 4);
$array_assoc_int2 = array ('three' => 3,
'four' => 4, 'five' => 5, 'six' => 6);
echo '<br><br>The given str&int ARRAY(1):<br>';
var_dump($array_assoc_int1);
echo '<br><br>The given str&int ARRAY(2):<br>';
var_dump($array_assoc_int2);
echo "<br><br>Test associative array with strings
as keys and integers as elements:<br>";
var_dump(array_diff($array_assoc_int1, $array_assoc_int2));
echo '<br><br>';
var_dump(array_diff($array_assoc_int2, $array_assoc_int1));
//Test associative array with strings as keys and elements
$array_assoc_str1 = array ('one' => 'un', 'two' =>
'deux', 'three' => 'trois', 'four' => 'quatre');
$array_assoc_str2 = array ('three' => 'trois',
'four' => 'quatre', 'five' => 'cinq', 'six' => 'six');
echo '<br><br>The given str&str ARRAY(1):<br>';
var_dump($array_assoc_str1);
echo '<br><br>The given str&str ARRAY(2):<br>';
var_dump($array_assoc_str2);
echo "<br><br>Test associative array with strings
as keys and integers as elements;<br>";
var_dump(array_diff($array_assoc_str1, $array_assoc_str2));
echo '<br><br>';
var_dump(array_diff($array_assoc_str2, $array_assoc_str1));
echo "<br><br>Test array_diff with more than 2 arguments:<br>";
var_dump(array_diff($array_int1, $array_int2,
$array_string1, $array_string2));
?>
EXERCISE
<?php
/*
* Test behaviour of array_diff()
* function with binary input
*/
echo "Testing usage variations;<br><br>";
$array1 = array( b"1",
b"hello",
"world",
"str1" => "hello",
"str2" => "world");
$array2 = array( b"1" => 'hello',
b"world",
"hello",
'test');
echo 'The given str ARRAY(1):<br>';
var_dump($array1);
echo '<br><br>The given str ARRAY(2):<br>';
var_dump($array2);
echo '<br><br>After array_diff($array1, $array2):<br>';
var_dump(array_diff($array1, $array2));
echo '<br><br>After array_diff($array2, $array1):<br>';
var_dump(array_diff($array2, $array1));
?>
EXERCISE
<?php
/*
* Test how array_diff() compares
* indexed arrays containing different
* data types as values in place of $arr1
*/
echo "Testing usage variations:<br>";
// Initialise function arguments
// not being substituted (if any)
$array = array(1, 2);
//get an unset variable
$unset_var = 10;
unset ($unset_var);
//get heredoc
$heredoc = <<<END
This is a heredoc
END;
//array of values to iterate over
$values = array(
/*1*/"empty array" => array(),
/*2*/
"int" => array(
// int data
0,
1,
12345,
-2345),
/*3*/
"float" => array(
// float data
10.5,
-10.5,
12.3456789000e10,
12.3456789000E-10,
.5),
/*4*/
"null" => array(
// null data
NULL,
null),
/*5*/
"boolean" => array(
// boolean data
true,
false,
TRUE,
FALSE),
/*6*/
"empty" => array(
// empty data
"",
''),
/*7*/
"string" => array(
// string data
"string",
'string',
$heredoc),
/*8*/
"binary" => array(
// binary data
b"binary",
(binary)"binary"),
/*9*/
"undefined" => array(
// undefined data
@$undefined_var),
/*10*/
"unset" => array(
// unset data
@$unset_var)
);
// loop through each element of the array for arr1
$iterator = 1;
foreach($values as $value) {
echo "<br><br>Iteration: $iterator<br>";
var_dump( array_diff($value, $array) );
$iterator++;
};
?>
EXERCISE
<?php
/*
* Test how array_diff() compares
* indexed arrays containing different
* data types as values in place of $arr2
*/
echo "Testing array_diff() : usage variations:<br>";
// Initialise function arguments
// not being substituted (if any)
$array = array(1, 2);
//get an unset variable
$unset_var = 10;
unset ($unset_var);
//get heredoc
$heredoc = <<<END
This is a heredoc
END;
//array of values to iterate over
$values = array(
/*1*/"empty array" => array(),
/*2*/
"int" => array(
// int data
0,
1,
12345,
-2345),
/*3*/
"float" => array(
// float data
10.5,
-10.5,
12.3456789000e10,
12.3456789000E-10,
.5),
/*4*/
"null" => array(
// null data
NULL,
null),
/*5*/
"boolean" => array(
// boolean data
true,
false,
TRUE,
FALSE),
/*6*/
"empty" => array(
// empty data
"",
''),
/*7*/
"string" => array(
// string data
"string",
'string',
$heredoc),
/*8*/
"binary" => array(
// binary data
b"binary",
(binary)"binary"),
/*9*/
"undefined" => array(
// undefined data
@$undefined_var),
/*10*/
"unset" => array(
// unset data
@$unset_var)
);
// loop through each element of the array for $arr2
$iterator = 1;
foreach($values as $value) {
echo "<br><br>Iteration: $iterator<br>";
var_dump( array_diff($array, $value) );
$iterator++;
};
?>
EXERCISE
<?php
/*
* Test how array_diff compares
* integers, floats and strings
*/
echo "Testing array_diff() : usage variations:<br><br>";
$arr_int = array(1, 2, 3);
$arr_float = array(1.00, 2.00, 3.00);
$arr_int_str = array('1', '2', '3');
$arr_float_str = array('1.00', '2.00', '3.00');
echo '<u>The given $arr_int:</u><br>';
var_dump($arr_int);
echo '<br><br><u>The given $arr_float:</u><br>';
var_dump($arr_float);
echo '<br><br><u>The given $arr_int_str:</u><br>';
var_dump($arr_int_str);
echo '<br><br><u>The given $arr_float_str:</u><br>';
var_dump($arr_float_str);
print "<br><br><u>Compare integers and floats.</u><br><br>";
echo 'After array_diff($arr_int, $arr_float):<br>';
var_dump(array_diff($arr_int, $arr_float));
echo '<br><br>After array_diff($arr_float, $arr_int):<br>';
var_dump(array_diff($arr_float, $arr_int));
print "<br><br><u>Compare integers and strings
containing an integers.</u><br><br>";
echo 'After array_diff($arr_int, $arr_int_str):<br>';
var_dump(array_diff($arr_int, $arr_int_str));
echo '<br><br>After array_diff($arr_int_str, $arr_int):<br>';
var_dump(array_diff($arr_int_str, $arr_int));
print "<br><br><u>Compare integers and strings
containing floats.</u><br><br>";
echo 'After array_diff($arr_int, $arr_float_str):<br>';
var_dump(array_diff($arr_int, $arr_float_str));
echo '<br><br>After array_diff($arr_float_str, $arr_int):<br>';
var_dump(array_diff($arr_float_str, $arr_int));
print "<br><br><u>Compare floats and strings
containing integers.</u><br><br>";
echo 'After array_diff(array_diff($arr_float, $arr_int_str):<br>';
var_dump(array_diff($arr_float, $arr_int_str));
echo '<br><br>After array_diff($arr_int_str, $arr_float):<br>';
var_dump(array_diff($arr_int_str, $arr_float));
print "<br><br><u>Compare floats and strings
containing floats.</u><br><br>";
echo 'After array_diff(array_diff($arr_float, $arr_float_str):<br>';
var_dump(array_diff($arr_float, $arr_float_str));
echo '<br><br>After array_diff($arr_float_str, $arr_float):<br>';
var_dump(array_diff($arr_float_str, $arr_float));
print "<br><br><u>Compare strings
containing integers and strings
containing floats.</u><br><br>";
echo 'After array_diff(array_diff($arr_int_str, $arr_float_str):<br>';
var_dump(array_diff($arr_int_str, $arr_float_str));
echo '<br><br>After array_diff($arr_float_str, $arr_int_str):<br>';
var_dump(array_diff($arr_float_str, $arr_int_str));
?>
EXERCISE
<?php
/*
* Test that array_diff behaves
* as expected for comparing:
* 1. the order of the array
* 2. duplicate values
* 3. duplicate key names
*/
echo "Testing array_diff() : usage variations:<br><br>";
$array_index = array('a', 'b', 'c', 0 => 'd', 'b');
//duplicate key (0), duplicate value (b)
$array_assoc = array ('2' => 'c',
//same key=>value pair, different order
'1' => 'b',
'0' => 'a',
'b' => '3',
//key and value from array_index swapped
'c' => 2);
//same as above, using integer
echo 'The given ARRAY(1):<br>';
var_dump($array_index);
echo '<br><br>The given ARRAY(2):<br>';
var_dump($array_assoc);
echo '<br><br>After array_diff($array_index, $array_assoc):<br>';
var_dump(array_diff($array_index, $array_assoc));
echo '<br><br>After array_diff($array_assoc, $array_index):<br>';
var_dump(array_diff($array_assoc, $array_index));
?>
EXERCISE
<?php
/*
* Test how array_diff compares arrays that
* 1. Contain referenced variables
* 2. Have been referenced to each other
*/
echo "Testing array_diff() : usage variations:<br><br>";
$a = 'a';
$arr1 = array ("&$a", 'b', 'c');
$arr2 = array (1, 2, 3);
echo "Basic Comparison.<br><br>";
echo 'After array_diff($arr1, $arr2):<br>';
var_dump(array_diff($arr1, $arr2));
echo '<br><br>After array_diff($arr2, $arr1):<br>';
var_dump(array_diff($arr2, $arr1));
$a = 1;
echo "<br><br>\$a changed.<br><br>";
echo 'After array_diff($arr1, $arr2):<br>';
var_dump(array_diff($arr1, $arr2));
echo '<br><br>After array_diff($arr2, $arr1):<br>';
var_dump(array_diff($arr2, $arr1));
$arr2 = &$arr1;
echo "<br><br>Arrays referenced to each other.<br><br>";
echo 'After array_diff($arr1, $arr2):<br>';
var_dump(array_diff($arr1, $arr2));
echo '<br><br>After array_diff($arr2, $arr1):<br>';
var_dump(array_diff($arr2, $arr1));
$arr1 = array('x', 'y', 'z');
echo "<br><br>\$arr1 changed.<br><br>";
echo 'After array_diff($arr1, $arr2):<br>';
var_dump(array_diff($arr1, $arr2));
echo '<br><br>After array_diff($arr2, $arr1):<br>';
var_dump(array_diff($arr2, $arr1));
?>
EXERCISE
<?php
/*
* Test how array_diff() compares
* multidimensional arrays
*/
echo "Testing array_diff() : usage variations:<br><br>";
$array1 = array('sub_array1' => array (1, 2, 3),
'sub_array2' => array ('a', 'b', 'c'));
$array2 = array('sub_arraya' => array (1, 3, 5),
'sub_arrayb' => array ('a', 'z', 'y'));
echo 'The given ARRRAY(1):<br>';
var_dump($array1);
echo '<br><br>The given ARRRAY(2):<br>';
var_dump($array2);
echo "<br><br>Compare two 2-D arrays.<br>";
var_dump(array_diff($array1, $array2));
var_dump(array_diff($array2, $array1));
echo "<br><br>Compare subarrays from two 2-D arrays.<br>";
var_dump(array_diff($array1['sub_array1'],
$array2['sub_arraya']));
var_dump(array_diff($array2['sub_arraya'],
$array1['sub_array1']));
var_dump(array_diff($array1['sub_array2'],
$array2['sub_arrayb']));
var_dump(array_diff($array2['sub_arrayb'],
$array1['sub_array1']));
echo "<br><br>Compare a subarray from one 2-D array
and one 2-D array.<br>";
var_dump(array_diff($array1['sub_array1'], $array2));
var_dump(array_diff($array1, $array2['sub_arraya']));
?>