How to use array intersect key in PHP

In this article, I will explain how the array_intersect_key() function can be used in PHP.
  • 2126

array_intersect_key() function in PHP

  • array_intersect_key() function is used to compare two or more arrays, if they are present in all of the other arrays, it returns an array with the keys and values from the first array.

  • In array_diff_key() function, only the key is used in the comparison.

Syntax

array_intersect_key(array1,array2,array3....)

Parameter

  • array1 array1 is required parameter. The first array is the array which will be compared with other arrays.

  • array2 array2 is required parameter. An array to be compared with the first array.

  • array3 array3 is optional parameter. An array to be compared with the first array.

Example

The following example show to how the array_intersect_key() function can be used in PHP.

<html>

<body>

<h3 style="color: red;">array_intersect_key() function example in PHP</h3>

    <?php

    $a1 = array('Nitin'  => 1, 'Sachin'  => 2, 'Priyanka'  => 3 );

    $a2 = array('Priyanka' => 4, 'Nitin' => 5, 'Ravi' => 6,);

    $result = array_intersect_key($a1, $a2);

    print_r($result);

    ?> 

</body>

</html>

 

Output

intersect-key.jpg

You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.