array_intersect_assoc
(PHP 4 >= 4.3.0, PHP 5)
array_intersect_assoc — 帶索引檢查計(jì)算數(shù)組的交集
說(shuō)明
array array_intersect_assoc ( array $array1
, array $array2
[, array $ ...
] )
array_intersect_assoc() 返回一個(gè)數(shù)組,該數(shù)組包含了所有在 array1
中也同時(shí)出現(xiàn)在所有其它參數(shù)數(shù)組中的值。注意和 array_intersect() 不同的是鍵名也用于比較。
參數(shù)
array1
The array with master values to check.
array2
An array to compare values against.
array
要對(duì)比的數(shù)組變量的列表。
返回值
返回一個(gè)數(shù)組,該數(shù)組包含了所有在 array1
中也同時(shí)出現(xiàn)在所有其它參數(shù)數(shù)組中的值。
范例
Example #1 array_intersect_assoc() 例子
<?php $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); $array2 = array("a" => "green", "b" => "yellow", "blue", "red"); $result_array = array_intersect_assoc($array1, $array2); print_r($result_array); ?>
以上例程會(huì)輸出:
Array ( [a] => green )
上面例子中可以看到只有鍵值對(duì) "a" => "green" 在兩個(gè)數(shù)組中都存在從而被返回。值 "red" 沒(méi)有被返回是因?yàn)樵?$array1 中它的鍵名是 0 而在 $array2 中 "red" 的鍵名是 1,鍵 "b" 沒(méi)有返回的原因是它的值和其他數(shù)組不同。
鍵值對(duì) key => value 中的兩個(gè)值僅在 (string) $elem1 === (string) $elem2 時(shí)被認(rèn)為相等。也就是說(shuō)使用了嚴(yán)格檢查,字符串的表達(dá)必須相同。
參見(jiàn)
array_intersect() - 計(jì)算數(shù)組的交集 array_uintersect_assoc() - 帶索引檢查計(jì)算數(shù)組的交集,用回調(diào)函數(shù)比較數(shù)據(jù) array_intersect_uassoc() - 帶索引檢查計(jì)算數(shù)組的交集,用回調(diào)函數(shù)比較索引 array_uintersect_uassoc() - 帶索引檢查計(jì)算數(shù)組的交集,用回調(diào)函數(shù)比較數(shù)據(jù)和索引 array_diff() - 計(jì)算數(shù)組的差集 array_diff_assoc() - 帶索引檢查計(jì)算數(shù)組的差集