PHP array_intersect_key() 函數(shù)
PHP array_intersect_key() 函數(shù)
實(shí)例
比較兩個(gè)數(shù)組的鍵名,并返回交集:
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","c"=>"blue","d"=>"pink");
$result=array_intersect_key($a1,$a2);
print_r($result);
?>
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","c"=>"blue","d"=>"pink");
$result=array_intersect_key($a1,$a2);
print_r($result);
?>
運(yùn)行實(shí)例 ?
定義和用法
array_intersect_key() 函數(shù)用于比較兩個(gè)(或更多個(gè))數(shù)組的鍵名 ,并返回交集。
該函數(shù)比較兩個(gè)(或更多個(gè))數(shù)組的鍵名,并返回一個(gè)交集數(shù)組,該數(shù)組包括了所有在被比較的數(shù)組(array1)中,同時(shí)也在任何其他參數(shù)數(shù)組(array2 或 array3 等等)中的鍵名。
語(yǔ)法
array_intersect_key(array1,array2,array3...)
參數(shù) | 描述 |
---|---|
array1 | 必需。與其他數(shù)組進(jìn)行比較的第一個(gè)數(shù)組。 |
array2 | 必需。與第一個(gè)數(shù)組進(jìn)行比較的數(shù)組。 |
array3,... | 可選。與第一個(gè)數(shù)組進(jìn)行比較的其他數(shù)組。 |
技術(shù)細(xì)節(jié)
返回值: | 返回一個(gè)交集數(shù)組,該數(shù)組包括了所有在被比較的數(shù)組(array1)中,同時(shí)也在任何其他參數(shù)數(shù)組(array2 或 array3 等等)中的鍵名。 |
---|---|
PHP 版本: | 5.1.0+ |
更多實(shí)例
實(shí)例 1
比較兩個(gè)數(shù)值數(shù)組的鍵名,并返回交集:
<?php
$a1=array("red","green","blue","yellow");
$a2=array("red","green","blue");
$result=array_intersect_key($a1,$a2);
print_r($result);
?>
$a1=array("red","green","blue","yellow");
$a2=array("red","green","blue");
$result=array_intersect_key($a1,$a2);
print_r($result);
?>
運(yùn)行實(shí)例 ?
實(shí)例 2
比較三個(gè)數(shù)組的鍵名,并返回交集:
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("c"=>"yellow","d"=>"black","e"=>"brown");
$a3=array("f"=>"green","c"=>"purple","g"=>"red");
$result=array_intersect_key($a1,$a2,$a3);
print_r($result);
?>
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("c"=>"yellow","d"=>"black","e"=>"brown");
$a3=array("f"=>"green","c"=>"purple","g"=>"red");
$result=array_intersect_key($a1,$a2,$a3);
print_r($result);
?>
運(yùn)行實(shí)例 ?
