PHP array_intersect_key() function
In this chapter you will learn:
- Definition for PHP array_intersect_key() function
- Syntax for PHP array_intersect_key() function
- Parameter for PHP array_intersect_key() function
- Return value for PHP array_intersect_key() function
- Example - Compare the keys of two indexed arrays, and return the matches
- Example - Compare the keys of three arrays, and return the matches
Definition
The array_intersect_key() function compares the keys of two or more arrays, and returns an array that contains the entries from array1 that are present in array2, array3, etc.
Syntax
PHP array_intersect_key() function has the following syntax.
array_intersect_key(array1,array2,array3...)
Parameter
Parameter | Is Required | Description |
---|---|---|
array1 | Required. | Array to be compared with |
array2 | Required. | Array to be compared with array1 |
array3,... | Optional. | Array to be compared with array1 |
Return
PHP array_intersect_key() function returns an array that contains the entries from array1 that are present in array2, array3, etc.
Example 1
Compare the keys of two indexed arrays, and return the matches:
<?php// ja va 2 s. c om
$a1=array("a"=>"A","b"=>"B","c"=>"C","d"=>"Dog");
$a2=array("a"=>"B","b"=>"New B","c"=>"java2s.com");
$result=array_intersect_key($a1,$a2);
print_r($result);
?>
The code above generates the following result.
Example 2
Compare the keys of three arrays, and return the matches:
<?php/* ja v a 2s . c o m*/
$a1=array("a"=>"A","b"=>"B","c"=>"C","d"=>"Dog");
$a2=array("a"=>"B","b"=>"New B","c"=>"java2s.com");
$a3=array("J"=>"java2s.com");
$result=array_intersect_key($a1,$a2,$a3);
print_r($result);
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP array_intersect_uassoc() Function
- Syntax for PHP array_intersect_uassoc() Function
- Parameter for PHP array_intersect_uassoc() Function
- Example - intersect associate array with user defined function
Home » PHP Tutorial » PHP Array Functions