array_udiff_assoc() Function
array_udiff_assoc()是PHP内建函数,用于区分两个或多个数组。该函数使用用户定义的带有附加键和返回差值的函数计算两个或多个数组的差值。它返回出现在第一个数组中的所有条目,而没有出现在任何其他数组中。它不同于array_diff_assoc()函数,因为它允许用户定义的函数来决定标准。
语法
1 | array_udiff_assoc($array1, $array2, $array3……….array nth, arr_udiffassocFun) |
使用的参数:array_udiff_assoc()函数参数描述如下:
- array1:它是初始数组,与另一个数组比较。它是强制性的。
- array2:与第一个数组键比较的数组。这是强制性的。
- array3:第二个数组与第一个数组键的比较。它是可选的。
- arr_udiffassocFun:它需要用户定义函数和定义用户定义回调函数的字符串,如果第一个参数<,=,或>大于0,则返回一个整数<,=,或>大于第二个参数。
返回值
返回一个数组,该数组包含第一个数组的元素,而其他数组中不包含这些元素。如果所有数组元素都存在,那么数组返回NULL。
注意:这个内置函数array_udiff_assoc())用于比较数组键和用户定义函数用于比较值。这个函数只检查一个n维数组的一维。
例子1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Input : $arr1 = array( "a" => "Geeks", "b" => "for", "d" => "geeks" ); $arr2 = array( "a" => "Geeks", "y" => "is", "d" => "geeks" ); Output: Array ( [b] => for ) Explanation: arr1 contains only one values(for) which is not present in arr2. |
例子2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | Input: $arr1 = array( "a" => "C", "b" => "C++", "d" => "Java", "x" => "XML", "y" => "C#" ); $arr2 = array( "a" => "C", "b" => "C++", "d" => "PHP", "x" => "Advanced PHP", "n" => "XML" ); Output: Array ( [d] => Java [x] => XML [y] => C# ) Explanation: arr1 return three values (article) which are not present in arr2. But XML is present both arr1 and arr2 but both have different key. |
让我们举一个简单的例子来理解array_udiff_assoc()函数。
程序1:取两个数组(array1和array2)并使用用户定义的键比较函数(arr_udiffassocFun)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?php // PHP code for array_uintersect_assoc function. // This function is used to decide which elements // to pick array_udiff_assoc function arr_udiffassocFun($x, $y) { return ($x === $y)? 0 : 1; } // array list for comparison. $arr1 = array( "a" => "Raj", "b" => "Ram", "d" => "Denish", "r" => "David" ); $arr2 = array( "a" => "Raj", "y" => "Ram", "d" => "Denish", "x" => "Ritche" ); // Driver code $result = array_udiff_assoc($arr1, $arr2, "arr_udiffassocFun"); print_r($result); ?> |
输出
1 2 3 4 5 | Array ( [b] => Ram [r] => David ) |
注:[b] => Ram打印,因为键不同。在array_diff_assoc()和array_udiff_assoc()中,会比较键和值。array_diff()只比较值。
程序2 接受4个数组(array1、array2、array3和array4),并使用用户定义的键比较函数array_udiff_assoc()。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?php // PHP code for array_uintersect_assoc function // This function is used to decide which elements // to pick array_udiff_assoc function arr_udiffassocFun($x, $y) { return ($x === $y)? 0 : 1; } // array list for comparison. $arr1 = array( "a" => "Larry", "b" => "Page", "d" => "Denish", "r" => "Ritche" ); $arr2 = array( "a" => "Larry", "y" => "Page", "d" => "Denish", "r" => "Ritche" ); $arr3 = array( "a" => "larry", "y" => "Bill Gate", "d" => "Denish", "r" => "Willion" ); $arr4 = array( "a" => "Raj", "y" => "Bill Gate", "d" => "Denish", "r" => "Woks" ); $result = array_udiff_assoc($arr1, $arr2, $arr3, $arr4, "arr_udiffassocFun"); // print result. print_r($result); ?> |
输出
1 2 3 4 | Array ( [b] => Page ) |
参考