array_uintersect_assoc()函数是PHP中的内置函数,用于为两个或多个数组的不同值计算键数组的交集。初始数组或第一个数组通过回调函数或用户定义的函数与所有其他数组进行比较,并返回匹配项。与array_uintersect()函数不同,键用于比较。
用法:
array array_uintersect_assoc( $array1, $array2, $array3... array nth, arr_uintersect_Function)
参数:该函数接受上述和以下描述的许多参数:
- $array1:它是要与另一个数组进行比较的初始数组。它是必填参数。
- $array2:与第一个数组键相比,它是第二个数组。它也是必填参数。
- $array3…:将数组与第一个数组键进行比较。它是可选参数。
- arr_uintersect_Function:它是必需的参数,用于保存用户定义的函数。它是一个字符串,用于定义用户定义的回调函数,并且如果第一个参数小于,等于或大于第二个参数,则返回小于,等于或大于零的整数。
返回值:它返回一个数组类型值,其中包含所有其他数组中存在的第一个数组。如果没有匹配项,则返回NULL。
注意:array_uintersect_assoc()函数比较数组的键,而用户定义的函数比较值。
例子:
Input : $arr1 = array( "a"=>"Website", "b"=>"frontend", "c"=>"programmer" ); $arr2 = array( "a"=>"Website", "b"=>"backend ", "c"=>"programmer" ); $arr3 = array( "a"=>"Website", "b"=>"fullstack ", "c"=>"programmer" ); $arr4 = array( "a"=>"Website", "b"=>"maintanance ", "c"=>"Team" ); Output : Array ( [a] => Website ) Explanation: Only one element (website) is common in all arrays. Input : $arr1 = array( "a"=>"Software", "b"=>"Testing", "c"=>"Tool" ); $arr2 = array( "a"=>"Software", "b"=>"Testing ", "c"=>"Team" ); Output : Array ( [a] => Software [b] => Testing ) Explanation: Two values are common in both array = Software, and Testing.
以下示例程序旨在说明PHP中的array_uintersect_assoc()函数:
程序1:该程序使用两个数组(array1和array2)和一个用户定义的键比较函数(arr_uintersect_Function)。
<?php
// PHP program for array_uintersect_assoc() function
function arr_uintersect_Function($a, $b) {
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
}
// Two array list with index and values
$arr1 = array (
"a" => "Java",
"b" => "Program",
"c" => "Practice",
"d" => "in",
"f" => "Geeksforgeeks"
);
$arr2 = array (
"a" => "Java",
"b" => "Code ",
"c" => "write",
"d" => "in",
"f" => "GeeksforgeeksIDE"
);
$result = array_uintersect_assoc($arr1, $arr2, "arr_uintersect_Function");
// Display result
print_r($result);
?>
输出:
Array ( [a] => Java [d] => in )
程序2:该程序使用两个数组(array1和array2)和一个用户定义的键比较函数(arr_uintersect_Function)。如果数组与任何键和值都不匹配,则返回NULL。
<?php
// PHP program for array_uintersect_assoc() function
// User-defined function
function arr_uintersect_Function($a, $b) {
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
}
// Two array list with index and values
$arr1 = array (
"a" => "my",
"b" => "best",
"c" => "programming",
"d" => "blog",
"e" => "Geeksforgeeks"
);
$arr2 = array (
"f" => "My",
"g" => "first ",
"h" => "programm",
"i" => "Geeks Hello"
);
$arr3 = array (
"j" => "Analysis",
"k" => "Algorithm ",
"l" => "and",
"m" => "Practice"
);
$result = array_uintersect_assoc( $arr1, $arr2, $arr3, "arr_uintersect_Function" );
// Display result
print_r($result);
?>
输出:
Array ( )
程序3:该程序返回所有参数中存在的$arr1的所有值。
<?php
$arr1 = array (
"a" => "gfg",
"b" => "ide",
"c" => "runcode"
);
$arr2 = array (
"a" => "GFG",
"B" => "practice"
);
$arr3 = array (
"a" => "Gfg",
"B" => "contribute"
);
print_r( array_uintersect_assoc($arr1, $arr2, $arr3, "strcasecmp") );
?>
输出:
Array ( [a] => gfg )
参考: https://www.php.net/manual/en/function.array-uintersect-assoc.php
相关用法
- p5.js sq()用法及代码示例
- d3.js d3.map.has()用法及代码示例
- PHP next()用法及代码示例
- p5.js day()用法及代码示例
- p5.js pow()用法及代码示例
- CSS var()用法及代码示例
- d3.js d3.map.set()用法及代码示例
- PHP pow( )用法及代码示例
- PHP pi( )用法及代码示例
- PHP Ds\Map get()用法及代码示例
- PHP Ds\Map put()用法及代码示例
- p5.js str()用法及代码示例
注:本文由纯净天空筛选整理自jit_t大神的英文原创作品 PHP | array_uintersect_assoc() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。