English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP array_intersect_key() Function Usage and Example

PHP Array Function Manual

The PHP array_intersect_key() function uses key names to calculate the intersection of arrays.

Syntax

array array_intersect_key ( array $array1, array $array2 [, array $array3 ...] );

Definition and Usage

It returns an array containing all the values of array1, which have matching keys in all parameters.

Parameter

Serial NumberParameters and Description
1

array1(required)

The first array is the array that other arrays will be compared with.

2

array2(required)

This is the array to be compared with the first array

3

array3(optional)

This is the array to be compared with the first array

Return value

Returns an associative array that contains all entries of array1, whose keys appear in all parameters. If there are any errors, it will return FALSE.

Online Example

Returns an array that contains all the values of $input1 that also appear in all other parameter arrays $input2.

<?php
   $input1 = array('black'  => 1, 'red'  => 2, 'green'  => 3 );
   $input2 = array('green' => 4, 'black' => 5, 'pink' => 6,);
   
   $result = array_intersect_key($input1, $input2);
   print_r($result);
?>
Test and see‹/›

Output result:

Array ( [black]  => 1 [green] => 3 )

 PHP Array Function Manual