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

array_diff_assoc() function in PHP


The array_diff_assoc() function compares array keys and values, and returns the differences. The array_diff() function only compares the values, whereas in array_diff_assoc() function both the keys and values are used for comparison.

Syntax

array_diff_assoc(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_assoc() function returns the comparison between both the arrays. It returns an array containing all the values from arr1 that are not present in any of the other arrays.

Example

<?php
   $arr1 = array("p"=>"football","q"=>"cricket","r"=>"hockey");
   $arr2 = array("s"=>"football","t"=>"cricket");
   $res = array_diff_assoc($arr1,$arr2);
   print_r($res);
?>

Output

Array
(
   [p] => football
   [q] => cricket
   [r] => hockey
)