PHP array_intersect_key() Function with Example

array_intersect_key() work is utilized to locate the coordinated components from at least two components (looks at keys).

Capacity “array_intersect_key()” thinks about the keys of the principal cluster with different exhibits and profits coordinated components based for the keys of the primary cluster.

Syntax:

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

Examples:

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 Code:

<?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:

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

Leave a Comment

error: Alert: Content is protected!!