Computer >> Computer tutorials >  >> Programming >> PHP

array_diff() function in PHP


The array_diff() function compares array values, and returns the differences. It returns an array containing the entries from the first array that are not present in any of the other arrays.

Syntax

array_diff(arr1, arr2, arr3,  arr4, …)

Parameters

  • arr1 − Array to compare from. Required.

  • arr2 − Array to compare against. Required.

  • arr3 − You can add more arrays to compare. Optional.

  • arr4 − You can add more arrays to compare. Optional.

Return

The array_diff() function returns an array containing the entries from the first array that are not present in any of the other arrays.

Example

<?php
   $arr1=array("p"=>"steve","q"=>"david","r"=>"peter");
   $arr2=array("s"=>"steve","t"=>"amy");
   $res = array_diff($arr1,$arr2);
   print_r($res);
?>

Output

Array
(
   [q] => david
   [r] => peter
)