Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP array_diff() Function

PHP array_diff() Function

PHP Array Reference ManualComplete PHP Array Reference Manual

Example

Compare two arrays by theirValue, and return the difference:

" 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 compares two (or more) arrays by theirValue, and returns the difference.

This function compares the values of two (or more) arrays (the value in key=>value) and returns a difference array containing all the values in the compared array (array1) that are not in any of the other argument arrays (array2 or array3 , etc.).


Syntax

array_diff(

array1,array2,array3...

);

Parameters Description
array1 Required. The first array to compare with the other arrays.
array2 Required. An array to compare with the first array.
array3,... Optional. Other arrays to compare with the first array.

Technical Details

Return Value: Returns a difference array containing all the keys and values in the compared array (array1) that are not in any of the other argument arrays (array2 or array3 , etc.).
PHP Version: 4.0.1+

More examples

Example 1

Compare three arrays by theirValue, and return the difference:

" 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 Reference ManualComplete PHP Array Reference Manual