PHP | array_intersect_assoc() Function
The array_intersect_assoc() is a builtin function in PHP and is used to compute the intersection of two or more arrays. This function is similar to the function array_intersect() which is discussed in the article PHP | array_intersect() function. The function is also used to compare the values of two or more arrays and returns the matches. The only difference is that the function returns all of the values of the first array that are present in all other arguments at the same index as that in first array, i.e., the keys are mainly used in the comparison.
Syntax:
array array_intersect_assoc($array1, $array2, $array3,...)
Parameters: The array_intersect_assoc() function takes atleast two arrays as parameter. The function can take any number of arrays as arguments greater than or equal to two.
Return Value: The function returns another array that contains the intersection of all the input arrays. If no element matches then, a NULL array is returned.
Examples:
Input : $array1 = ("1" => "shyam", "2" => "rishav", "3" => "gaurav"); $array2 = ("1" => "shyam", "2" => "rishi", "3" => "rishav"); $array3 = ("1" => "shyam", "2" => "rishav", "3" => "ravi"); Output : Array ( [1] => shyam )
In the below program, we have used array_intersect_assoc() to find the intersection between arrays. Let’s look closer at the outputs of this and array_intersect() function.
<?php // PHP function to illustrate the use of array_intersect_assoc() function Intersect( $array1 , $array2 , $array3 ) { $result = array_intersect_assoc ( $array1 , $array2 , $array3 ); return ( $result ); } $array1 = array ( "1" => "shyam" , "2" => "rishav" , "3" => "gaurav" ); $array2 = array ( "1" => "shyam" , "2" => "rishi" , "3" => "rishav" ); $array3 = array ( "1" => "shyam" , "2" => "rishav" , "3" => "ravi" ); print_r(Intersect( $array1 , $array2 , $array3 )); ?> |
Output:
Array ( [1] => shyam )
In the above program we have used the array_intersect_assoc() to find the intersection of the arrays. In the below program we will use the array_intersect() function to do the same. Give close attention to the outputs of both the programs. The first one returns only those elements which are strictly similar, both by values and keys, unlike array_intersect().
<?php // PHP function to illustrate the use of array_intersect() function Intersect( $array1 , $array2 , $array3 ) { $result = array_intersect ( $array1 , $array2 , $array3 ); return ( $result ); } $array1 = array ( "1" => "shyam" , "2" => "rishav" , "3" => "gaurav" ); $array2 = array ( "1" => "shyam" , "2" => "rishi" , "3" => "rishav" ); $array3 = array ( "1" => "shyam" , "2" => "rishav" , "3" => "ravi" ); print_r(Intersect( $array1 , $array2 , $array3 )); ?> |
Output:
Array ( [1] => shyam [2] => rishav )
Reference: http://php.net/manual/en/function.array-intersect-assoc.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- PHP | tan( ) Function
- PHP | Ds\Set contains() Function
- PHP | end() Function
- PHP | pos() Function
- PHP | key() Function
- PHP | Ds\Map put() Function
- p5.js | value() Function
- p5.js | max() function
- PHP | Ds\Map xor() Function
- D3.js | d3.rgb() Function
- p5.js | pow() function
- p5.js | day() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.