PHP array_diff Function | Compute Differences in Arrays

PHP array_diff Function is an inbuilt function in PHP which computes the difference between two or more arrays. It returns an array containing elements which exist in the first array but not in the subsequent arrays. In this article, we will discuss PHP array_diff Function along with common use cases.

Syntax

array_diff(array1,array2,array3…);

Parameters

PHP array_diff Function 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.

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]).

Note: This function preserves the keys of the first array. An example will demonstrate this later.

PHP array_diff Function Diagram

Examples

Let’s go through a few examples to demonstrate the working of PHP array_diff Function.

Example 1: Integer Arrays

<?php

	//Array containing 1 to 10 integers
	$testArray1 = range(1,10); //Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 ) 

	//Array containing 5 to 15 integers
	$testArray2 = range(5,15); //Array ( [0] => 5 [1] => 6 [2] => 7 [3] => 8 [4] => 9 [5] => 10 [6] => 11 [7] => 12 [8] => 13 [9] => 14 [10] => 15 )

	
	//Integers present in array1 but not in array2
	$finalArray1 = array_diff($testArray1, $testArray2);
	print_r($finalArray1);
	/*	
	Array
	(
	    [0] => 1
	    [1] => 2
	    [2] => 3
	    [3] => 4
	)
	*/

	
	//Integers i sent in array2 but not in array1
	$finalArray2 = array_diff($testArray2, $testArray1);
	print_r($finalArray2);
	/*
	Array
	(
	    [6] => 11
	    [7] => 12
	    [8] => 13
	    [9] => 14
	    [10] => 15
	)
	*/

?>

The above example demonstrates the working of PHP array_diff Function with integer arrays. 2 arrays are created containing integer values from 1 to 10 and 5 to 15 respectively. Firstly, the elements which are present in the first array but not in the second array are printed. Secondly, the elements present in the second array but not in the first array are printed.

Example 2: String Arrays

Similarly, we can extend the above example to work with string arrays as well.

<?php
	$fruits1 = array('apple', 'mango', 'banana');

	$fruits2 = array('mango', 'melon');

	//Elements present in array1 but not in array2
	print_r(array_diff($fruits1, $fruits2));
	/*
	Array
	(
	    [0] => apple
	    [2] => banana
	)
	*/

	//Elements present in array2 but not in array1
	print_r(array_diff($fruits2, $fruits1));
	/*
	Array
	(
	    [1] => melon
	)
	*/

?>

Example 3: Multiple Arrays

For instance, consider the following example with multiple arrays.

<?php
	
	$fruits1 = array('apple', 'mango', 'banana', 'melon', 'grapes');

	$fruits2 = array('mango', 'melon');

	$fruits3 = array('grapes');

	$fruits4 = array('mango');

	print_r(array_diff($fruits1, $fruits2, $fruits3, $fruits4));
	/*
	Array
	(
	    [0] => apple
	    [2] => banana
	)
	*/

?>

The final array consists of elements present in the first array but not in the subsequent arrays. Also, you can observe that array_diff preserves the keys of the existing array.

Example 4: Associative Arrays

<?php
	$color1 = array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
	$color2 = array("e"=>"red","f"=>"black","g"=>"purple");
	$color3 = array("a"=>"red","b"=>"black","h"=>"yellow");

	$result=array_diff($color1,$color2,$color3);
	print_r($result);
	/*

	Array
	(
	    [b] => green
	    [c] => blue
	)


	*/
?>

The above example demonstrates the working of PHP array_diff Function with associative arrays. Again, it can be shown that it preserves the keys of the original array.

Conclusion

In conclusion, we discussed the PHP array_diff Function in this article. It is very useful in computing the differences between two or more arrays. You can also read about PHP array_diff_assoc function which is a bit different that this one.

You can explore other PHP Array Functions. Also, you can read about array_diff function on the Official PHP Documentation.

Spread the Knowledge

Leave a Reply

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