比较键和值两个数组,并返回匹配项(使用用户定义的键比较函数):
<?php
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("d"=>"red","b"=>"green","e"=>"blue");
$result=array_intersect_uassoc($a1,$a2,"myfunction");
print_r($result);
?>
亲自试一试 »
array_intersect_uassoc() 函数比较键和值两个(或多个)数组,并返回匹配项。
笔记:该函数使用用户定义的函数来比较按键!
此函数比较两个或多个数组的键和值,并返回一个包含以下条目的数组数组1存在于数组2,数组3, ETC。
array_intersect_uassoc(
array1, array2, array3, ..., myfunction)
Parameter | Description |
---|---|
array1 | Required. The first array is the array that the others will be compared with |
array2 | Required. An array to be compared with the first array |
array3,... | Optional. An array to be compared with the first array |
myfunction | Required. A string that define a callable comparison function. The comparison function must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument |
返回值: | 返回一个包含条目的数组数组1存在于所有其他数组中 |
---|---|
PHP 版本: | 5+ |
比较键和值三个数组(使用用户定义的函数来比较键),并返回匹配项:
<?php
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","b"=>"green","d"=>"blue");
$a3=array("e"=>"yellow","a"=>"red","d"=>"blue");
$result=array_intersect_uassoc($a1,$a2,$a3,"myfunction");
print_r($result);
?>
亲自试一试 »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!