PHP array_diff()Functions

PHP Array 参考手册Complete PHP Array Reference Manual

Example

Compare two arrays'Value, and return the difference:

<?php $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("e"=>"red","f"=>"green","g"=>"blue"); $result=array_diff($a1,$a2); print_r($result); ?>

Run example »

Definition and Usage

The array_diff() function is used to compare two (or more) arrays'Value, and return the difference.

This function compares the values (value in key=>value) of two (or more) arrays, and returns a difference array that includes all values in the compared array (array1) but not in any other parameter array (array2orarray3etc.).


Syntax

array_diff(array1,array2,array3...);

Parameter Description
array1 Required. The first array to be compared with other arrays.
array2 Required. The array to be compared with the first array.
array3,... Optional. Other arrays to be compared with the first array.

Technical Details

Return value: Returns a difference array that includes all values in the compared array (array1) but not in any other parameter array (array2orarray3etc.).
PHP Version: 4.0.1+


More Examples

Example 1

Compare three arrays'Value, and return the difference:

<?php $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("e"=>"red","f"=>"black","g"=>"purple"); $a3=array("a"=>"red","b"=>"black","h"=>"yellow"); $result=array_diff($a1,$a2,$a3); print_r($result); ?>

Run example »


PHP Array 参考手册Complete PHP Array Reference Manual Other Extensions