PHP array_diff() Function Example

PHP Script:


<?php
$arr1=array("a"=>"Monkey","b"=>"Lion","c"=>"Tiger","d"=>"Cat");
$arr2=array("e"=>"Dog","f"=>"Donkey","g"=>"Horse","h"=>"Monkey");

$result=array_diff($arr1,$arr2);
print_r($result);

$arr3 = array("CE","EE","TE");
$arr4 = array("EE","BE","ME");
$arr5 = array("IM","TE","ME");

$result=array_diff($arr3,$arr4,$arr5);
print_r($result);
?>

Output:


Array
(
[b] => Lion
[c] => Tiger
[d] => Cat
)
Array
(
[0] => CE
)

Description:

PHP array_diff() function is used to compare the values of two or more arrays and returns the differences as an array which contains the elements from an Array1 that are not present in other arrays.

PHP array_diff() function parameters:

1- Array1 (required)
2- Array2 (required)
3- Array3 (optional)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s