Computer >> Computer tutorials >  >> Programming >> PHP

array_intersect_key() function in PHP


The array_intersect_key() function compares array keys, and returns the matches. It returns an array containing all of the values in the first array whose values exist in all of the parameters.

Syntax

array_intersect_key(arr1, arr2, arr3,  arr4, …)

Parameters

  • arr1 − Array to compare from. Required.

  • arr2 − Array to compare against. Required.

  • arr3 − You can add more arrays to compare. Optional.

  • arr4 − You can add more arrays to compare. Optional.

Return

The array_intersect_key() function returns an array containing all of the values in the first array whose values exist in all of the parameters.

Example

<?php
   $arr1 = array("p"=>"headphone","q"=>"earpod","r"=>"charger");
   $arr2 = array("p"=>"headphone","q"=>"earpod");
   $res = array_intersect_key($arr1,$arr2);
   print_r($res);
?>

Output

Array
(
   [p] => headphone
   [q] => earpod
)