English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP array_diff() Function Usage and Example

PHP Array Function Manual

Definition and Usage

array_diff()The function compares arrays and returns the difference between two arrays (only compares key-value). It will compare array1Compares one or more arrays and returns array1does not appear in any other array.

Syntax

array array_diff ( array $array1, array $array2 [, array $array3 ...] );

Parameter

Serial NumberParameters and Description
1

array1 (Required)

This is the first array to compare with the other arrays passed to the function.

2

array2 (Required)

This is the array to compare with the first array

3

array3(optional)

This is the second array to compare with the first array

4

More Arrays (optional)

You can pass more arrays to compare with the first input array.

Return Value

PHP Array Functions array_diff()Returns an array that includes all the values that are in array1 but not in any other parameter array. Note that the key names are retained.

PHP version

This function was first introduced in PHP version4.0.1introduced.

Online Example

See the following example of the array_diff() function returning the difference between two arrays-

<?php
   $array1 = array("orange", "banana", "apple");
   $array2 = array("orange", "mango", "apple");
   print_r(array_diff($array1, $array2));
?>
Test to see‹/›

Output Result:

Array 
( 
    [1] => banana 
)

Online Example

 $array1Multiple matches are processed in the same way in the example below. See the following example−

<?php
   $array1 = array("a" => "green", "red", "blue", "red");
   $array2 = array("b" => "green", "yellow", "red");
   print_r(array_diff($array1, $array2));
?>
Test to see‹/›

Output Result:

Array 
( 
    [1] => blue 
)

PHP Array Function Manual