当前位置:Gxlcms > PHP教程 > php数组入门教程之关联数组的交集

php数组入门教程之关联数组的交集

时间:2021-07-01 10:21:17 帮助过:29人阅读

本文介绍下,有关php数组中求关联数组交集的方法,有需要的朋友参考下。

php函数array_intersect_assoc()与array_intersect()基本相同,只不过它在比较中还考虑了数组的键。 因此,只有在第一个数组中出现,且在所有其他输入数组中也出现的键/值对才返回到结果数组中。

形式如下: array array_intersect_assoc(array array1,array array2[,arrayN…])

例子,返回出现在$fruit1数组中,也同时出现在$fruit2与$fruit3中的所有键/值对:

"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 )  
?>

人气教程排行