array_diff() function in php

array_diff() function in php

by Pooja Chikara 27-Mar-20

0 236


array_diff() function of PHP compares the values of two and more arrays and return the difference in the form of new array. It is an inbuilt function of PHP.

The resulting array contains the values of first array that are not matches to other arrays values.

Syntax
array_diff ($arr1,$arr2,.....,$arrN);

Here

$arr1,$arr2 and $rrN are arrays.


Example 1:

<?php
$arr1=array("Mango","Orange","Pear","Banana","Apple");
$arr2=array("Mango","Banana","Apple");
$new_arr=array_diff($arr1,$arr2);
echo "<pre>";
print_r($new_arr);
?>

Output:

array_diff() function in php


Example 2:

<?php
$arr1=array("Mango","Orange","Pear","Banana","Apple");
$arr2=array("Mango","Banana","Apple");
$arr3=array("Orange","Mango");
$new_arr=array_diff($arr1,$arr2,$arr3);
echo "<pre>";
print_r($new_arr);
?>

Output:

array_diff() function in php


Share:


Leave a Reply


Comments
    Waiting for your comments