"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("e"=>"red","f"=>"green","g"=>"blue"); $result=array_diff($a1,$a2); print_r($result); ?> ```"> "red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("e"=>"red","f"=>"green","g"=>"blue"); $result=array_diff($a1,$a2); print_r($result); ?> ```" />
Easy Tutorial
❮ Func Math Sqrt Func String Str Split ❯

PHP array_diff() Function

Complete PHP Array Reference Manual

Example

Compare the values of two arrays 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);
?>

Definition and Usage

The array_diff() function is used to compare the values of two (or more) arrays and return the difference.

This function compares the values (value in key=>value) of two (or more) arrays and returns an array that contains all the values from the first array that are not present in any of the other arrays.


Syntax

Parameter Description
array1 Required. The first array to compare with the other arrays.
array2 Required. An array to compare against the first array.
array3,... Optional. Additional arrays to compare against the first array.

Technical Details

Return Value: Returns an array containing all the values from the first array that are not present in any of the other arrays.
PHP Version: 4.0.1+
--- ---

More Examples

Example 1

Compare the values of three arrays 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);
?>

❮ Func Math Sqrt Func String Str Split ❯