PHP Tutorial

PHP Examples

PHP Practice

PHP Array array_intersect_key() Function (With Examples)

In this tutorial, we will learn about the PHP array_intersect_key() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP array_intersect_key() Function

The array_intersect_key() function is used to find the matched elements from two or more elements (compares keys). Function array_intersect_key() compares the keys of the first array with the other arrays and returns matched elements based on the keys of the first array.

Syntax

The syntax of the array_intersect_key() function:

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

Parameters

The parameters of the array_intersect_key() function:

  • array1, array2 are the input arrays other arrays are an optional.

Return Value

The return type of this method is array, it returns an associative array containing all the entries of array which have keys that are present in all arguments. [Source]

Sample Input/Output

Input:
$arr1 = array("name" => "Prem", "age" => 28, "city" => "Gwalior");
$arr2 = array("name" => "Amit", "age" => 23, "gender" => "Male");

Function calling: 
array_intersect_key($arr1, $arr2);

Output:
Array
(
    [name] => Prem
    [age] => 28
)

PHP array_intersect_key() Function Example

<?php

$arr1 = array("name" => "Prem", "age" => 28, "city" => "Gwalior");
$arr2 = array("name" => "Amit", "age" => 23, "gender" => "Male");

//finding matched elements
$ans = array_intersect_key($arr1, $arr2);

print_r ($ans);
?>

Output

The output of the above example is:

Array
(
    [name] => Prem
    [age] => 28
)

To understand the above example, you should have the basic knowledge of the following PHP topics:

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.