R4R Team working to provide free of cost for experienced and freshers

Tutorials | Examples|Interview Questions and Answers|Java Tutorials| C Tutorials | C++ Tutorials | Spring Tutorials| Hibernate Tutorials | JSP Tutorials | Servelets Tutorials| C# Tutorials | PHP Tutorials etc , Basics | Download Software | Data Types | Variables | Constant | Storage & Classes | Operators | Decisions Making | Loops | Functions| Numbers | Arrays | Pointers | String | Datastructure | Expressions | Preprocessor Statement | I/O | Exception Handling | OOPs Tutorial | Classes and object |C++ Inheritance Overloading | Polymorphism | Abstraction | Encapsulation | Interfaces | Multithreading etc

PHP Programing language

array_intersect_key() Functions in PHP
Previous Home Next

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