w3resource

PHP: array_diff() function

PHP: Computes the difference of arrays

The array_diff() function is used to compares an array against one or more other arrays and returns the values in the first array that are not present in any of the other arrays.

Version:

(PHP 4 and above)

Syntax:

array_diff(array1, array2, array3 ...)

Parameter:

Name Description Required /
Optional
Type
array1 The specified array which will be compared with other arrays. Required Array
array2 Compared with the first array. Required Array
array3 Compared with the first array. Optional Array

Return value:

An array containing all the entries from array1 whose values are not present in any of the other arrays i.e. array2, array3 etc.

Value Type: Array

Example:

<?php
$array1 = array(0=> 'Language', 1=>'Math', 2=>'Science', 3=>'Geography');
$array2 = array(0=> 'Math', 1=>'Science', 2=>'History');
$diff_result = array_diff($array1, $array2);
print_r($diff_result);
?>

Output:

Array ( [0] => Language [3] => Geography) 

Pictorial Presentation:

php function reference: array_diff() function

View the example in the browser

Practice here online:

See also

PHP Function Reference

Previous:array_diff_ukey
Next: array_fill



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/php/function-reference/array_diff.php