Wednesday, 24 September 2014

array_diff_assoc in PHP

PHP array_diff_assoc() function compares first array with another arrays and returns the difference and array keys are used in the comparison. array_diff_assoc() and array_diff() are different functions.
PHP array_diff_assoc() function is suitable for only associative array.

Syntax:
array_diff_assoc(array1,array2,array3...)

Parameters Description:

array1 : Required. The first array is compared with the others arrays.
array2 : Required. An array to be compared with the first array.
array3 : Optional. An array to be compared with the first array.

Tip: The first array compares with single array or many arrays.

Note: Both the key and the value is used in the comparison.

Example:


<?php
$daysarr1 = array(0=>"Sunday",1=>"Monday",2=>"Tuesday");
$daysarr2 = array(0=>"Thursday",1=>"Tuesday",2=>"Monday");
$daysarr3 = array(0=>"Wednesday",1=>"Monday",2=>"Sunday");
print_r( array_diff_assoc($daysarr1,$daysarr2,$daysarr3) );
?>

O/P:


Array (
          [0] => Sunday 
          [2] => Tuesday 
        )

0 comments:

Post a Comment