PHP array_diff_assoc() Function

What is PHP array_diff_assoc() Function?

If you want to know what exist in one array and that doesn’t exist in other arrays (one or more arrays), use array_diff_assoc() function. This difference is made in both keys and values. So, one element is considered exist if the same element with same key and same value is found in another array.

Syntax:

array_diff_assoc(array1, array2, array3, …)

Parameters:

The Function has 2 required parameters and any number of optional parameters-

array1 (Required): This is the first array whose elements the function checks whether exist in other arrays or not.

array2 (Required): It is another array whose elements the function compares with the first array. Check example 1.

array3, … (Optional): It is one or more arrays whose elements the function compares with the first array. Check example 2.

Return Values:

The function returns an array which contains elements that exist in first array and don’t exist in other arrays.

  • If values are same but keys are not same in the comparing arrays, the function considers it non-exists. Check example 3.
  • If values and keys are same but case are different in the comparing arrays, the function considers it non-exists. Check example 4.

Examples:

Example 1:

<pre>
<?php
$array1 = ['c' => 'CSS', 'h' => 'HTML',  'j' => 'JavaScript', 'p' => 'PHP', 'c' => 'C#', 'g' => 'Go', 'j' => 'Java', 'n' => 'NoSQL'];
$array2 = ['c' => 'CSS', 'h' => 'HTML',  'j' => 'JavaScript', 'p' => 'PHP'];
$last_array = array_diff_assoc($array1, $array2);
print_r($last_array);
?>
</pre>

Output:

Array
(
    => C#
    [j] => Java
    [g] => Go
    [n] => NoSQL
)

Example 2:

<pre>
<?php
$array1 = ['c' => 'CSS', 'h' => 'HTML',  'j' => 'JavaScript', 'p' => 'PHP', 'c' => 'C#', 'g' => 'Go', 'j' => 'Java', 'n' => 'NoSQL'];
$array2 = ['c' => 'CSS', 'h' => 'HTML',  'j' => 'JavaScript', 'p' => 'PHP'];
$array3 = ['c' => 'CSS', 'c' => 'C#', 'g' => 'Go', 'j' => 'Java'];
$last_array = array_diff_assoc($array1, $array2, $array3);
print_r($last_array);
?>
</pre>

Output:

Array(
    [n] => NoSQL
)

Example 3:

<pre>
<?php
$array1 = ['c' => 'CSS', 'j' => 'JavaScript'];
$array2 = ['s' => 'CSS', 's' => 'JavaScript'];
$last_array = array_diff_assoc($array1, $array2);
print_r($last_array);
?>
</pre>

Output:

Array
(
    => CSS
    [j] => JavaScript
)

Example 4:

<pre>
<?php
$array1 = ['c' => 'CSS', 'j' => 'JavaScript'];
$array2 = ['c' => 'CSS', 'j' => 'Javascript'];
$last_array = array_diff_assoc($array1, $array2);
print_r($last_array);
?>
</pre>

Output:

Array
(
    [j] => JavaScript
)

Example 5:

<pre>
<?php
$array1 = ['j' => 'JavaScript', 'c' => ['C', 'C++', 'C#'], 'p' => 'PHP'];
$array2 = ['c' => 'CSS', 'c' => ['C', 'C++']];
$last_array = array_diff_assoc($array1['c'], $array2['c']);
print_r($last_array);
?>
</pre>

Output:

Array
(
    [2] => C#
)

Notes on array_diff_assoc() Function:

  • The function checks elements in one-dimension. If you want to compare elements inside inner levels of multidimensional arrays, you can mention those as the parameters in the function. Check example 5.

PHP Version Support:

PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8

Summary: PHP array_diff_assoc() Function

array_diff_assoc() is a built-in array function which is a very effective function to compare elements between arrays.

Reference:

https://www.php.net/manual/en/function.array-diff-assoc.php