Wednesday, 24 September 2014

array_diff_key in PHP

 array_diff_key() tutorial will explain you how to compare arrays using keys in PHP with an example.
PHP array_diff_key() function compares two or more arrays, and returns an array with the keys and values from the first array, only if they are not available in other arrays.

Syntax: array_diff_key(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: Compare the first array with one array or more arrays.
Note: Only the key is used in the comparison.

Example:

<?php
$daysarr1 = array(0=>"Sunday",1=>"Monday",2=>"Tuesday");
$daysarr2 = array(2=>"Thursday",3=>"Wednesday",4=>"Friday");
$daysarr3 = array(5=>"Tuesday",6=>"Monday",7=>"Thursday");
print_r( array_diff_key($daysarr1,$daysarr2,$daysarr3) );
?>

O/P:



Array (
          [0] => Sunday
          [1] => Monday
        )

0 comments:

Post a Comment