Wednesday, 24 September 2014

array_intersect_key in PHP

array_intersect_key() function returns a result array with keys and values from the first input array, only if the key is present in all of the other input arrays.

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

Parameters Description:
array1 : Required. The first array which will be compared with other input arrays
array2 : Required. An array to compare with first array
array3 : Optional. An array to be compare with first array
Tip : You can compare the first array with one array, or as many as you like.

Example:

<?php
$daysarr1 = array(0=>"Sunday",1=>"Monday",2=>"Tuesday");
$daysarr2 = array(2=>"Wednesday",3=>"Thursday",4=>"Friday");
$daysarr3 = array(2=>"Monday",1=>"Saturday",8=>"Wednesday");
print_r( array_intersect_key($daysarr1,$daysarr2,$daysarr3) );
?>

O/P:


Array ( 
        [2] => Tuesday
       )

0 comments:

Post a Comment