Home  >  Article  >  Backend Development  >  PHP array introductory tutorial: Intersection of associative arrays

PHP array introductory tutorial: Intersection of associative arrays

WBOY
WBOYOriginal
2016-07-25 08:57:57940browse
This article introduces the method of finding the intersection of associative arrays in PHP arrays. Friends in need can refer to it.

The PHP function array_intersect_assoc() is basically the same as array_intersect(), except that it also considers the keys of the array in the comparison. Therefore, only key/value pairs that appear in the first array and also appear in all other input arrays are returned in the result array.

The form is as follows: array array_intersect_assoc(array array1,array array2[,arrayN…])

Example, return all key/value pairs that appear in the $fruit1 array and also appear in $fruit2 and $fruit3:

<?php
//求关联数组的交集
$fruit1 = array("red"=>"Apple","yellow"=>"Banana","orange"=>"Orange");  
$fruit2 = array("yellow"=>"Pear","red"=>"Apple","purple"=>"Grape");  
$fruit3 = array("green"=>"Watermelon","orange"=>"Orange","red"=>"Apple");  
$intersection = array_intersect_assoc($fruit1, $fruit2, $fruit3);  
print_r($intersection); //by bbs.it-home.org

// output  
// Array ( [red] => Apple )  
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn