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()function compares arrays and returns the difference between two arrays (only compares key-value). It will compare the array1Compares one or more arrays passed to it and returns the 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 be compared with the other arrays passed to the function.

2

array2 (Required)

This is the array to be compared with the first array

3

array3(Optional)

This is the second array to be compared 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 in the 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 array_diff returning the difference between two arrays-

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

Output Result:

Array 
( 
    [1]]=>banana 
)

Online Example

 $array1Multiple matches in the example are processed in the same way. 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 and See‹/›

Output Result:

Array 
( 
    [1]]=>blue 
)

PHP Array Function Manual