PHP: array_intersect_key() function
PHP: Computes the intersection of arrays using keys for comparison
The array_intersect_key() is used to create an array containing keys and values of the first array whose keys (i.e. from the first array) are present in all other arrays.
Version:
(PHP 4 and above)
Syntax:
array_intersect_key(array1, array2,....)
Parameters:
Name | Description | Required / Optional |
Type |
---|---|---|---|
array1 | The first array. | Required | Array |
array2 | An array to check keys against the first array. | Required | Array |
Return value:
An associative array containing all the values of array1 which have matching keys that are present in all arguments.
Value Type: Array
Example:
<?php
$array1 = array("Orange" => 1, "Apple" => 2, "Banana" => 3, "Chery" =>4);
$array2 = array("Orange" => 5, "Banana" => 6, "Mango" =>7);
var_dump(array_intersect_key($array1, $array2));
?>
Output:
array(2) { ["Orange"]=> int(1) ["Banana"]=> int(3)}
Pictorial Presentation:

View the example in the browser
Practice here online :
See also
Previous:array_intersect_ assoc
Next: array_intersect_ uassoc
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join