PHP | array_udiff_uassoc()函数
array_udiff_uassoc()函数是PHP中的一个内置函数,用于区分两个或多个数组。该函数通过使用两个带有附加索引键的用户定义函数来计算差异数组。它使用回调函数比较数据和索引并返回差异。
句法 :
array_udiff_uassoc($arr1, $arr2, $arr3........nth array, value_Function, key_Function )
使用的参数:这个array_udiff_uassoc()函数参数描述如下:
- $arr1, $arr2, $arr3, $arr4,…., $arrn :这表示我们要区分的数组列表。
- value_Function:该参数代表用户自定义函数。此用户定义函数将用于比较值。
- key_Function:也是用户自定义函数。此用户定义函数用于比较数组键。
注意:如果第一个参数是 <、= 或 > 而不是第二个参数,则两个比较函数(value_Function、key_Function)都返回一个整数 <、= 或 >,而不是 0。
返回值:此函数返回一个数组,其中包含数组$arr1中不存在于任何其他数组中的所有值。
一些相关功能:
- PHP | arr_diff()函数:计算数组的差异。
- PHP | arr_udiff()函数:通过使用的用户自定义回调函数计算数组的差异并比较数据。
- PHP | array_diff_assoc()函数:计算带有附加索引键的数组的差异。
- PHP | array_diff_uassoc()函数:此函数比较一个或多个数组之间的键和值,并从第一个数组返回其余数组中不存在的元素。
- PHP | array_diff_key()函数:将第一个参数数组的键与其余数组进行比较,并返回一个数组,其中包含 $array1 中不存在于任何其他数组中的所有条目。
下面的程序说明了 array_udiff_uassoc()函数。
方案一:
PHP
$b) ? 1 : -1;
}
// comparison function for array keys
function key_Function($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
}
// array1 list for comparison.
$arr1 = array(
"m" => "C lab",
"n" => "Java lab",
"o" => "C# lab",
"x" => "C++ lab",
"y" => "Ruby lab",
);
//array2 list for comparison.
$arr2 = array(
"m" => "C lab",
"b" => "Java lab",
"c" => "C# lab",
"x" => "C++ lab",
"n" => "Ruby lab",
);
$result = array_udiff_uassoc($arr1,
$arr2, "value_Function", "key_Function");
// print result.
print_r($result);
?>
PHP
$b) ? 1 : -1;
}
// comparison function for array keys
function key_Function($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
}
// array1 list for comparison.
$arr1 = array(
"a" => "C lab",
"b" => "Java lab",
"c" => "C# lab",
"d" => "C++ lab",
"e" => "Ruby lab",
);
// array2 list for comparison.
$arr2 = array(
"a" => "C lab",
"b" => "Java lab",
"c" => "C# lab",
"d" => "C++ lab",
"e" => "XML lab",
);
$arr3 = array(
"a" => "C lab",
"b" => "Java lab",
"c" => "C# lab",
"d" => "C++ lab",
"e" => "CSS lab"
);
$arr4 = array(
"a" => "C lab",
"b" => "Java lab",
"c" => "C# lab",
"d" => "C++ lab",
"e" => "PHP lab"
);
$result = array_udiff_uassoc($arr1, $arr2,
$arr3, $arr4, "value_Function", "key_Function");
// print result.
print_r($result);
?>
PHP
$b) ? 1 : -1;
}
// comparison function for array keys
function key_Function($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
}
// array1 list for comparison.
$arr1 = array(
"x" => "Geeks",
"y" => "for",
"z" => "Geeks",
);
// array2 list for comparison.
$arr2 = array(
"x" => "Geeks",
"y" => "for",
"z" => "Geeks",
);
$result = array_udiff_uassoc($arr1,
$arr2, "value_Function", "key_Function");
// print result.
print_r($result);
?>
PHP
$b) ? 1 : -1;
}
// comparison function for array keys
function key_Function($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
}
// array1 list for comparison.
$arr1 = array(
"a" => "C lab",
"b" => "Java lab",
"d" => "C# lab",
);
// array2 list for comparison.
$arr2 = array(
"a" => "C ",
"b" => "Java ",
"d" => "C#",
);
// array3 list for comparison.
$arr3 = array(
"a" => "Program",
"b" => "Code",
"d" => "Run",
);
$result = array_udiff_uassoc($arr1,
$arr2, $arr3, "value_Function", "key_Function");
// print result.
print_r($result);
?>
输出:
Array
(
[n] => Java lab
[o] => C# lab
[y] => Ruby lab
)
节目:2
PHP
$b) ? 1 : -1;
}
// comparison function for array keys
function key_Function($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
}
// array1 list for comparison.
$arr1 = array(
"a" => "C lab",
"b" => "Java lab",
"c" => "C# lab",
"d" => "C++ lab",
"e" => "Ruby lab",
);
// array2 list for comparison.
$arr2 = array(
"a" => "C lab",
"b" => "Java lab",
"c" => "C# lab",
"d" => "C++ lab",
"e" => "XML lab",
);
$arr3 = array(
"a" => "C lab",
"b" => "Java lab",
"c" => "C# lab",
"d" => "C++ lab",
"e" => "CSS lab"
);
$arr4 = array(
"a" => "C lab",
"b" => "Java lab",
"c" => "C# lab",
"d" => "C++ lab",
"e" => "PHP lab"
);
$result = array_udiff_uassoc($arr1, $arr2,
$arr3, $arr4, "value_Function", "key_Function");
// print result.
print_r($result);
?>
输出:
Array
(
[e] => Ruby lab
)
节目:3
PHP
$b) ? 1 : -1;
}
// comparison function for array keys
function key_Function($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
}
// array1 list for comparison.
$arr1 = array(
"x" => "Geeks",
"y" => "for",
"z" => "Geeks",
);
// array2 list for comparison.
$arr2 = array(
"x" => "Geeks",
"y" => "for",
"z" => "Geeks",
);
$result = array_udiff_uassoc($arr1,
$arr2, "value_Function", "key_Function");
// print result.
print_r($result);
?>
输出:
Array
(
)
程序: 4取三个数组(array1和array2,array3)并使用比较函数array_udiff_uassoc()函数。如果所有三个数组具有相同的索引但值不同,则返回第一个数组。
PHP
$b) ? 1 : -1;
}
// comparison function for array keys
function key_Function($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
}
// array1 list for comparison.
$arr1 = array(
"a" => "C lab",
"b" => "Java lab",
"d" => "C# lab",
);
// array2 list for comparison.
$arr2 = array(
"a" => "C ",
"b" => "Java ",
"d" => "C#",
);
// array3 list for comparison.
$arr3 = array(
"a" => "Program",
"b" => "Code",
"d" => "Run",
);
$result = array_udiff_uassoc($arr1,
$arr2, $arr3, "value_Function", "key_Function");
// print result.
print_r($result);
?>
输出:
Array
(
[a] => C lab
[b] => Java lab
[d] => C# lab
)
参考: 函数 : PHP 。 PHP