array_diff_assocCOMPUTES the difference between ARRAYS with additional index check.
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.
Deeper dimensions can be made using this function to compare identical keys.
<?php
arr array_diff_assoc ( 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
$arra01a = [ "Real Madrid Club de Fútbol",
"Fútbol Club Barcelona",
"Club Atlético de Madrid",
"Deportivo Alavés",
"Getafe Club de Fútbol" ];
$arra01b = [ "Club Atlético de Madrid",
"Deportivo Alavés",
"Getafe Club de Fútbol" ];
$arra01c = [ "Rayo Vallecano de Madrid",
"Real Betis Balompié",
"Real Valladolid Club de Fútbol" ];
echo 'The first given ARRAY<br>';
var_dump($arra01a);
echo '<br><br>The second given ARRAY<br>';
var_dump($arra01b);
echo '<br><br>The third given ARRAY<br>';
var_dump($arra01c);
$arra01abc1 =
array_diff_assoc($arra01a, $arra01b, $arra01c);
echo '<br><br>After
array_diff_assoc($arra01a, $arra01b, $arra01c):<br>';
var_dump($arra01abc1);
$arra01abc2 =
array_diff_assoc($arra01a, $arra01c, $arra01b);
echo '<br><br>After
array_diff_assoc($arra01a, $arra01c, $arra01b):<br>';
var_dump($arra01abc2);
$arra01abc3 =
array_diff_assoc($arra01b, $arra01c, $arra01a);
echo '<br><br>After
array_diff_assoc($arra01b, $arra01c, $arra01a):<br>';
var_dump($arra01abc3);
$arra01abc4 =
array_diff_assoc($arra01c, $arra01a, $arra01b);
echo '<br><br>After
array_diff_assoc($arra01c, $arra01a, $arra01b):<br>';
var_dump($arra01abc4);
?>
EXERCISE
<?php
$arra02a = [ "Real Madrid Club de Fútbol",
"Fútbol Club Barcelona",
"Club Atlético de Madrid",
"Deportivo Alavés",
"Getafe Club de Fútbol" ];
$arra02b = [ "CLUB ATLÉTICO DE MADRID",
"DEPORTIVO ALAVÉS",
"GETAFE CLUB DE FÚTBOL" ];
$arra02c = [ "Rayo Vallecano de Madrid",
"Real Betis Balompié",
"Real Valladolid Club de Fútbol" ];
echo 'The first given ARRAY<br>';
var_dump($arra02a);
echo '<br><br>The second given ARRAY<br>';
var_dump($arra02b);
echo '<br><br>The third given ARRAY<br>';
var_dump($arra02c);
$arra02abc1 =
array_diff_assoc($arra02a, $arra02b, $arra02c);
echo '<br><br>After
array_diff_assoc($arra02a, $arra02b, $arra02c):<br>';
var_dump($arra02abc1);
$arra02abc2 =
array_diff_assoc($arra02a, $arra02c, $arra02b);
echo '<br><br>After
array_diff_assoc($arra02a, $arra02c, $arra02b):<br>';
var_dump($arra02abc2);
$arra02abc3 =
array_diff_assoc($arra02b, $arra02c, $arra02a);
echo '<br><br>After
array_diff_assoc($arra02b, $arra02c, $arra02a):<br>';
var_dump($arra02abc3);
$arra02abc4 =
array_diff_assoc($arra02c, $arra02a, $arra02b);
echo '<br><br>After
array_diff_assoc($arra02c, $arra02a, $arra02b):<br>';
var_dump($arra02abc4);
?>
EXERCISE
<?php
$arra03a = [ 'Madrid' => "Real Madrid Club de Fútbol",
'Barcelona' => "Fútbol Club Barcelona",
'Villarreal' => "Villarereal Club de Fútbol" ];
$arra03b = [ 'Madrid' => "REAL MADRID CLUB DE FÚTBOL",
'Barcelona' => "Fútbol Club Barcelona" ,
'Vitoria' => "Deportivo Alavés",
'Getafe' => "Getafe Club de Fútbol" ];
$arra03c = [ 'Madrid' => "Rayo Vallecano de Madrid",
'Sevilla' => "Real Betis Balompié",
'Valladolid' => "Real Valladolid Club de Fútbol" ];
echo 'The first given ARRAY<br>';
var_dump($arra03a);
echo '<br><br>The second given ARRAY<br>';
var_dump($arra03b);
echo '<br><br>The third given ARRAY<br>';
var_dump($arra03c);
$arra03abc1 =
array_diff_assoc($arra03a, $arra03b, $arra03c);
echo '<br><br>After
array_diff_assoc($arra03a, $arra03b, $arra03c):<br>';
var_dump($arra03abc1);
$arra03abc2 =
array_diff_assoc($arra03a, $arra03c, $arra03b);
echo '<br><br>After
array_diff_assoc($arra03a, $arra03c, $arra03b):<br>';
var_dump($arra03abc2);
$arra03abc3 =
array_diff_assoc($arra03b, $arra03c, $arra03a);
echo '<br><br>After
array_diff_assoc($arra03b, $arra03c, $arra03a):<br>';
var_dump($arra03abc3);
$arra03abc4 =
array_diff_assoc($arra03c, $arra03a, $arra03b);
echo '<br><br>After
array_diff_assoc($arra03c, $arra03a, $arra03b):<br>';
var_dump($arra03abc4);
?>
?>
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 first given ARRAY<br>';
var_dump($array1);
echo '<br><br>The second given ARRAY<br>';
var_dump($array2);
echo '<br><br>The third given ARRAY<br>';
var_dump($array3);
$result[] = array_diff_assoc($array1, $array2);
$result[] = array_diff_assoc($array1, $array3);
$result[] = array_diff_assoc($array2, $array3);
$result[] = array_diff_assoc($array1, $array2, $array3);
echo '<br><br>The resulting ARRAY<br>';
print_r($result);
?>
EXERCISE
<?php
/*
* Test basic functionality of
* array_diff_assoc
*/
echo "Testing array_diff_assoc() : basic functionality:<br><br>";
$array_default_key = array('one', 2, 'three', '4');
$array_numeric_key = array(1 => 'one', 2=> 'two', 3 => 4);
$array_string_key = array('one' => 1, 'two' => '2', '3' => 'three');
echo 'The first given ARRAY<br>';
var_dump($array_default_key);
echo '<br><br>The second given ARRAY<br>';
var_dump($array_numeric_key);
echo '<br><br>The third given ARRAY<br>';
var_dump($array_string_key);
echo "<br><br><br>Compare Default keys to numeric keys.<br><br>";
echo 'After array_diff_assoc($array_default_key,
$array_numeric_key):<br>';
var_dump(array_diff_assoc($array_default_key,
$array_numeric_key));
echo '<br><br>After array_diff_assoc($array_numeric_key,
$array_default_key):<br>';
var_dump(array_diff_assoc($array_numeric_key,
$array_default_key));
echo "<br><br>Compare Default keys to string keys.<br><br>";
echo 'After array_diff_assoc($array_default_key,
$array_numeric_key):<br>';
var_dump(array_diff_assoc($array_default_key,
$array_numeric_key));
echo '<br><br>After array_diff_assoc($array_numeric_key,
$array_default_key):<br>';
var_dump(array_diff_assoc($array_numeric_key,
$array_default_key));
echo "<br><br>Compare numeric keys to string keys.<br><br>";
echo 'After array_diff_assoc$array_numeric_key,
$array_string_key):<br>';
var_dump(array_diff_assoc($array_numeric_key,
$array_string_key));
echo '<br><br>After array_diff_assoc($array_string_key,
$array_numeric_key):<br>';
var_dump(array_diff_assoc($array_string_key,
$array_numeric_key));
?>
EXERCISE
<?php
/*
* Test how array_diff_assoc()
* compares binary data
*/
echo " Testing array_diff_assoc() : 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 $array1:<br>';
var_dump($array1);
echo '<br><br>The given $array2:<br>';
var_dump($array2);
echo '<br><br><br>After array_diff_assoc($array1, $array2):<br>';
var_dump(array_diff_assoc($array1, $array2));
echo '<br><br>After array_diff_assoc($array2, $array1):<br>';
var_dump(array_diff_assoc($array2, $array1));
?>
EXERCISE
<?php
/*
* Test how array_diff_assoc()
* compares arrays containing different
* data types as keys
*/
echo "Testing array_diff_assoc() : usage variations.<br><br>";
$array = array(1, 2, 3);
//get an unset variable
$unset_var = 10;
unset ($unset_var);
// heredoc string
$heredoc = <<<EOT
hello world
EOT;
//Different data types as keys to be passed to $arr1 argument
$inputs = array(
// int data
/*1*/
'int' => array(
0 => 'zero',
1 => 'one',
12345 => 'positive',
-2345 => 'negative'),
// float data
/*2*/
'float' => array(
10.5 => 'float 1',
-10.5 => 'float 2',
.5 => 'float 3'),
// null data
/*3*/
'null' => array(
NULL => 'null 1',
null => 'null 2'),
// boolean data
/*4*/
'bool' => array(
true => 'boolt',
false => 'boolf',
TRUE => 'boolT',
FALSE => 'boolF'),
// empty data
/*5*/
'empty' => array(
"" => 'emptyd',
'' => 'emptys'),
// string data
/*6*/
'string' => array(
"string" => 'stringd',
'string' => 'strings',
$heredoc => 'stringh'),
// binary data
/*7*/
'binary' => array(
b"binary1" => 'binary 1',
(binary)"binary2" => 'binary 2'),
// undefined data
/*8*/
'undefined' => array(
@$undefined_var => 'undefined'),
// unset data
/*9*/
'unset' => array(
@$unset_var => 'unset'),
);
echo 'The given $array:<br>';
var_dump($array);
echo '<br><br>The given $inputs:<br>';
var_dump($inputs);
// loop through each element of $inputs
// to check the behavior of array_diff_assoc
$iterator = 1;
foreach($inputs as $key => $input) {
echo "<br><br>Iteration: $iterator<br>";
var_dump( array_diff_assoc($input, $array));
$iterator++;
};
?>
EXERCISE
<?php
/*
* Test how array_diff_assoc
* compares integers, floats and string
*/
echo "Testing array_diff_assoc() : usage variations.<br><br>";
$arr_default_int = array(1, 2, 3, 'a');
$arr_float = array(0 => 1.00, 1.00 => 2.00, 2.00 => 3.00, 'b');
$arr_string = array('1', '2', '3', 'c');
$arr_string_float = array('0' => '1.00',
'1.00' => '2.00',
'2.00' => '3.00', 'd');
echo 'The given $arr_default_int:<br>';
var_dump($arr_default_int);
echo '<br><br>The given $arr_float:<br>';
var_dump($arr_float);
echo '<br><br>The given $arr_string:<br>';
var_dump($arr_string);
echo '<br><br>The given $arr_string_float:<br>';
var_dump($arr_string_float);
echo "<br><br><br>Result of comparing
integers and floating point numbers.<br>";
echo 'After array_diff_assoc($arr_default_int, $arr_float):<br>';
var_dump(array_diff_assoc($arr_default_int, $arr_float));
echo '<br><br>After
array_diff_assoc($arr_float, $arr_default_int):<br>';
var_dump(array_diff_assoc($arr_float, $arr_default_int));
echo "<br><br><br>Result of comparing
integers and strings containing an integers.<br>";
echo 'After array_diff_assoc($arr_default_int, $arr_string):<br>';
var_dump(array_diff_assoc($arr_default_int, $arr_string));
echo '<br><br>After
array_diff_assoc($arr_string, $arr_default_int):<br>';
var_dump(array_diff_assoc($arr_string,
$arr_default_int));
echo "<br><br><br>Result of comparing
integers and strings containing floating points.<br>";
echo 'After
array_diff_assoc($arr_default_int, $arr_string_float):<br>';
var_dump(array_diff_assoc($arr_default_int,
$arr_string_float));
echo '<br><br>After
array_diff_assoc($arr_string_float, $arr_default_int):<br>';
var_dump(array_diff_assoc($arr_string_float,
$arr_default_int));
echo "<br><br><br>Result of comparing
floating points and strings containing integers.<br>";
echo 'After
array_diff_assoc($arr_float, $arr_string):<br>';
var_dump(array_diff_assoc($arr_float, $arr_string));
echo '<br><br>After
array_diff_assoc($arr_string, $arr_float):<br>';
var_dump(array_diff_assoc($arr_string, $arr_float));
echo "<br><br><br>Result of comparing floating
points and strings containing floating point.<br>";
echo 'After
array_diff_assoc($arr_float, $arr_string_float):<br>';
var_dump(array_diff_assoc($arr_float, $arr_string_float));
echo '<br><br>After
array_diff_assoc($arr_string_float, $arr_float):<br>';
var_dump(array_diff_assoc($arr_string_float, $arr_float));
echo "<br><br><br>Result of comparing
strings containing integers and
strings containing floating points.<br>";
echo 'After
array_diff_assoc($arr_string, $arr_string_float):<br>';
var_dump(array_diff_assoc($arr_string, $arr_string_float));
echo '<br><br>After
array_diff_assoc($arr_string_float, $arr_string):<br>';
var_dump(array_diff_assoc($arr_string_float, $arr_string));
echo "<br><br><br>Result of comparing
more than two arrays.<br>";
echo 'After
array_diff_assoc($arr_default_int, $arr_float,
$arr_string, $arr_string_float):<br>';
var_dump(array_diff_assoc($arr_default_int,
$arr_float,
$arr_string,
$arr_string_float));
?>
EXERCISE
<?php
/*
* Test how array_diff_assoc behaves
* 1. When comparing an array that
* has similar elements
* but has been created in a different order
* 2. When doing a strict comparison of
* string representation
*/
echo "Testing array_diff_assoc() : usage variations.<br><br>";
$array = array ('zero',
1 => 1,
'two' => 2.00000000000001);
$inputs = array (
//default keys => string values
/*1*/ array('2.00000000000001', '1', 'zero', 'a'),
//numeric keys => string values
/*2*/ array(2 => '2.00000000000001',
1 => '1',
0 => 'zero',
3 => 'a'),
//string keys => string values
/*3*/ array('2' => '2.00000000000001',
'1' => '1',
'0' => 'zero',
'3' => 'a') ,
//default keys => numeric values
/*4*/ array(2, 1, 0),
//numeric keys => numeric values
/*5*/ array(2 => 2,
1 => 1,
0 => 0),
//string keys => numeric values
/*6*/ array('two' => 2,
'1' => 1,
'0' => 0),
//default keys => float values
/*7*/ array(2.00000000000001, 1.00, 0.01E-9),
//numeric keys => float values
/*8*/ array(2 => 2.00000000000001,
1 => 1.00,
0 => 0.01E-9),
//string keys => float values
/*9*/ array ('two' => 2.00000000000001,
'1' => 1.00,
'0' =>0.01E-9)
);
echo 'The given $array:<br>';
var_dump($array);
echo '<br><br>The given $inputs:<br>';
var_dump($inputs);
// loop through each element of $inputs
// to check the behavior of array_diff_assoc
$iterator = 1;
foreach($inputs as $input) {
echo "<br><br>Iteration: $iterator<br>";
var_dump(array_diff_assoc($array, $input));
var_dump(array_diff_assoc($input, $array));
$iterator++;
};
?>
EXERCISE
<?php
/*
* Tests how array_diff_assoc compares
* 1. Referenced variables
* 2. Arrays that have been referenced to each other
*/
echo "Testing array_diff_assoc() : usage variations.<br><br>";
$a = 'a';
$arr1 = array('a', 'b', 'c', $a);
$arr2 = array('a' => 1, 'b' => 2, 'c' => 3, &$a);
echo 'The given $arr1:<br>';
var_dump($arr1);
echo '<br><br>The given $arr2:<br>';
var_dump($arr2);
echo "<br><br><br><u>Results when \$a = $a</u>:<br>";
echo 'After array_diff_assoc($arr1, $arr2);<br>';
var_dump(array_diff_assoc($arr1, $arr2));
echo '<br><br>After array_diff_assoc($arr2, $arr1);<br>';
var_dump(array_diff_assoc($arr2, $arr1));
$a = 4;
echo "<br><br><br><u>Results when \$a has been changed to $a</u>:<br>";
echo 'After array_diff_assoc($arr1, $arr2);<br>';
var_dump(array_diff_assoc($arr1, $arr2));
echo '<br><br>After array_diff_assoc($arr2, $arr1);<br>';
var_dump(array_diff_assoc($arr2, $arr1));
$arr2 = &$arr1;
echo "<br><br><br><u>Results when \$arr2 is referenced to \$arr1</u>:<br>";
echo 'After array_diff_assoc($arr1, $arr2);<br>';
var_dump(array_diff_assoc($arr1, $arr2));
echo '<br><br>After array_diff_assoc($arr2, $arr1);<br>';
var_dump(array_diff_assoc($arr2, $arr1));
$arr1 = array('zero' => 'x', 'one' => 'y', 'two' => 'z');
echo "<br><br><br><u>Results when \$arr1 is changed to</u>:<br>";
var_dump($arr1);
echo '<br><br>After array_diff_assoc($arr1, $arr2);<br>';
var_dump(array_diff_assoc($arr1, $arr2));
echo '<br><br>After array_diff_assoc($arr2, $arr1);<br>';
var_dump(array_diff_assoc($arr2, $arr1));
?>
EXERCISE
<?php
/*
* Test how array_diff_assoc()
* behaves when comparing:
* 1. the order of the array
* 2. duplicate values
* 3. duplicate key names
*/
echo "Testing array_diff_assoc() : variation.<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_index:<br>';
var_dump($array_index);
echo '<br><br>The given $array_assoc:<br>';
var_dump($array_assoc);
echo '<br><br>After
array_diff_assoc($array_index, $array_assoc):<br>';
var_dump(array_diff_assoc($array_index, $array_assoc));
echo '<br><br>After
array_diff_assoc($array_assoc, $array_index):<br>';
var_dump(array_diff_assoc($array_assoc, $array_index));
?>
EXERCISE
<?php
/*
* Test how array_diff_assoc
* behaves when comparing
* multi-dimensional arrays
*/
echo "Testing array_diff_assoc() : usage variations:<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 $array1:<br>';
var_dump($array1);
echo '<br><br>The given $array2:<br>';
var_dump($array2);
echo "<br><br><br><u>Compare two 2-D arrays</u>:<br>";
var_dump(array_diff_assoc($array1, $array2));
echo '<br><br>';
var_dump(array_diff_assoc($array2, $array1));
echo "<br><br><br><u>Compare subarrays from two 2-D arrays</u>:<br>";
var_dump(array_diff_assoc($array1['sub_array1'],
$array2['sub_arraya']));
echo '<br><br>';
var_dump(array_diff_assoc($array2['sub_arraya'],
$array1['sub_array1']));
echo '<br><br>';
var_dump(array_diff_assoc($array1['sub_array2'],
$array2['sub_arrayb']));
echo '<br><br>';
var_dump(array_diff_assoc($array2['sub_arrayb'],
$array1['sub_array1']));
echo "<br><br><br><u>Compare a subarray
from one 2-D array and one 2-D array</u>:<br>";
var_dump(array_diff_assoc($array1['sub_array1'], $array2));
echo '<br><br>';
var_dump(array_diff_assoc($array1, $array2['sub_arraya']));
?>