|
但實際應用中也遇到了一些小問題,正如下:
實例:
復制代碼 代碼如下:
<?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()函數適用于比較兩個數組鍵值的交集,返回的并不只有鍵值,而是鍵值和對應的數組值。
php技術:PHP數組的交集array_intersect(),array_intersect_assoc(),array_inter_key()函數的小問題,轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。