PHP Programing language

adplus-dvertising
array_intersect_key() Functions in PHP
Previous Home Next
adplus-dvertising

array_intersect_key()

The array_intersect_key() function compares the keys of two or more arrays, and return an array that contains the entries from any array1 that are present in any array2, array3, and so on.

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

In above syntax,"array1" is the array that the others will be compared with "array2" is the array to be compared with the first array "array3,..." is the array to be compared with the first array

Example:

<?php
$array1=array("a"=>"Apple","b"=>"Banana","c"=>"Mango");
$array2=array("a"=>"Apple","c"=>"Banana","d"=>"Mango");
$result=array_intersect_key($array1,$array2);
print_r($result);
?>

Output:

Array ( [a] => Apple [c] => Mango )

array_intersect_ukey()

array_intersect_key() compares the keys of two or more arrays, and return an array that contains the entries from any array1 that are present in any array2, array3, and so on.

Syntax:
array_intersect_ukey(array1,array2,array3...,myfunction)

In above syntax,"array1" is the array that the others will be compared with "array2" is the array to be compared with the first array "array3,..." is the array to be compared with the first array and "myfunction" is a string that define a callable comparison function. The comparison function must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.

Example:

<?php
function myfunction($a,$b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}
$a1=array("a"=>"Apple","b"=>"Banana","c"=>"Orange");
$a2=array("a"=>"Orange","b"=>"Mango","e"=>"Orange");
$result=array_intersect_ukey($a1,$a2,"myfunction");
print_r($result);
?>

Output:

Array ( [a] => Apple [b] => Banana )

Previous Home Next