PHP array_diff_assoc Function | Compute Difference in Array

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.

Syntax

array_diff_assoc(array1,array2,array3…);

Parameters

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.

Return Value

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[0], $array2[0]).

Examples

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] => 0
    		[1] => 1
    	)
	*/
?>

Conclusion

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.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *