返回一個交集共有元素的數組(只是數組值得比較)、array_intersect_assoc()函數是將索引值和值綁定,一起比較交集部分、array_intersect_key()函數是將兩個數組的索引值進行比較,返回索引值交集的數組。
但實際應用中也遇到了一些小問題,正如下:
執行個體:
複製代碼 代碼如下:<?PHP
$array = array("red"=>"Red","green"=>"red4","Red15"=>"Red",7=>"Level","Width"=>"Red","azzzz1"=>"art","peak"=>158);
$array1 = array("red"=>"Red2","greena"=>"red","Red15"=>"Red",7=>"Level","Width"=>"Red","azzzz"=>"art","peak"=>158);
$num = array_intersect($array,$array1);
print_r ($num);
echo "<br />";
$num = array_intersect_assoc($array,$array1);
print_r($num);
echo "<br />";
$num = array_intersect_key($array,$array1);
print_r ($num);
?>
運行結果: 複製代碼 代碼如下:Array ( [red] => Red [Red15] => Red [7] => Level [Width] => Red [azzzz1] => art [peak] => 158 )
Array ( [Red15] => Red [7] => Level [Width] => Red [peak] => 158 )
Array ( [red] => Red [Red15] => Red [7] => Level [Width] => Red [peak] => 158 )
總結:
1.array_intersect()函數進行的比較只有數組值的比較,而且存在如”Red“和”Red2“比較時會返回"Red",反之則不會返回"Red2";
2.array_intersect_assoc()函數是將數組的值與索引值一起比較,而且不會存在array_intersect的情況,適用於較嚴格的比較;
3.array_intersect_key()函數適用於比較兩個數組索引值的交集,返回的並不只有索引值,而是索引值和對應的數組值。