This article defines an array function of PHP, which is used to find the difference between two arrays. The details of function are given below :
array_diff() : array_diff() is used to compare two or more arrays to find the difference. The function takes at least two arrays as arguments and returns an array containing values from first array which does not matched with values of second (or other) array.
Syntax : array_diff(array1, array2, array3,.......)
Description :
array1 : Specifies an array to compare with other arrays.(Required)
array2 : Specifies an array to compare with array1.(Required)
array3 : Specifies another array which also compare with array1.(Optional)
Note : The function checks only one dimension with multi-dimensional array.
Example1 :
<?php
$metro_cities=array("a"=>"Delhi","b"=>"Mumbai","c"=>"Chennai","d"=>"Kolkata");
$cities=array("a"=>"Mumbai","b"=>"Lucknow","c"=>"Kolkata","d"=>"Bhopal");
print_r(array_diff($metro_cities,$cities));
?>
Output : Array ( [a] => Delhi [c] => Chennai )
Example 2 :
<?php
$arr1 = array(2, 5, 6, 7, 14, 16);
$arr2 = array(0, 2, 3, 8, 10, 15);
$arr3 = array(1, 6, 8, 12, 16, 20);
print_r(array_diff($arr1,$arr2,$arr3));
?>
Output : Array ( [1] => 5 [2] => 7 [3] => 14 )
0 Comment(s)