PHP array_diff_assoc Function is an inbuilt function in PHP which computes the difference between two or more arrays. Unlike array_diff Function, this function computes the difference taking both keys and values into account. It compares the keys and values in input arrays and returns an array containing elements present in the first array and not in subsequent arrays. In this article, we will discuss PHP array_diff_assoc Function. Also, we will cover a few examples to demonstrate it’s usage.
The array_diff_assoc Function in PHP consists of 2 mandatory parameters. However, you can provide N number of parameters to the function. The description of the parameters is as follows:
- array1: The first array to compare from. This parameter is mandatory.
- array2: The second array to compare against. This parameter is also mandatory.
- arrayN: Also, More arrays can be passed to compare against. Optional.
The PHP array_diff Function returns an array containing elements which are present in the first array but not in the subsequent arrays. Additionally, it compares the arrays both on keys and values present in them.
Also, This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1, $array2).
Let’s go through a few examples to demonstrate the working of PHP array_diff_assoc Function.
Example 1: Basic Example
For instance, let’s consider a basic example using array_diff_assoc function.
<?php $testArray1 = array('a' => 'apple', 'b' => 'banana', 'c' => 'cat'); $testArray2 = array('a' => 'apple', 'b' => 'banana', 'd' => 'dog'); print_r(array_diff_assoc($testArray1, $testArray2)); /* Array ( [c] => cat ) */ ?>
In the above example, you can observe that only c=>cat is present in the first array and not in the second array. Both a=>apple and b=>banana are present in both the arrays. On the other hand, d=>dog is only present in the second array.
Example 2: Same Key, Different Value
For example, consider a case when both the arrays have a common key but different values. We modify the above example to change the value present in key ‘a’ in the second array.
<?php $testArray1 = array('a' => 'apple', 'b' => 'banana', 'c' => 'cat'); $testArray2 = array('a' => 'aeroplane', 'b' => 'banana', 'd' => 'dog'); print_r(array_diff_assoc($testArray1, $testArray2)); /* Array ( [a] => apple [c] => cat ) */ ?>
You can observe that although key ‘a’ is present in both the arrays, it has different values in the arrays. As a result, the value of the key ‘a’ is preserved from the first array.
Example 3: Equality Checks
Two values from key => value pairs are considered equal only if (string) $elem1 === (string) $elem2 . Let’s consider an example to demonstrate this:
<?php $array1 = array(0, 1, 2); $array2 = array("00", "01", "2"); $result = array_diff_assoc($array1, $array2); print_r($result); /* Array (  => 0  => 1 ) */ ?>
We discussed the array_diff_assoc Function in PHP. It is similar to the array_diff Function but compares the arrays for both keys and values. You can also go through other PHP Array Functions on Concatly.
Additionally, do visit the Official Documentation of PHP for more information on array_diff_assoc Function.
Vishesh is currently working as an Intermediate Software Engineer with Orion Health, New Zealand. He graduated with a Masters in Information Technology from the University of Auckland in 2021. With more than 4 years of work experience, his expertise includes Java, Python, Machine Learning, PHP, Databases, Design and Architecture.