Home » PHP

PHP array_intersect_key() Function with Example

PHP array_intersect_key() Function: Here, we are going to learn about the array_intersect_key() function with example.
Submitted by IncludeHelp, on February 10, 2019

PHP array_intersect_key() Function

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:

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

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

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
)




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.