array_uintersect_assoc() Function
array_uintersect_assoc()函数是PHP中的一个内建函数,用于计算两个或多个数组的键和不同值的交集。初始数组或第一个数组通过回调函数或用户定义函数与所有其他数组进行比较,并返回匹配。与array_uintersect()不同的是,这些键用于比较。
语法
1 | array_uintersect_assoc($array1, $array2, $array3..........array nth, arr_uintersectFunction) |
使用的参数:该函数接受两个参数,两个参数都是强制的,其他参数是可选的,参数描述如下:
- $array1:它是初始数组,并与另一个数组进行比较。这是强制性的。
- $array2 :将数组与第一个数组键进行比较。这是强制性的。
- $array3 :第二个数组与第一个数组键比较。其可选的。
- arr_uintersectFunction :它需要的用户定义函数和一个字符串,该字符串定义用户定义的回调函数,如果第一个参数小于、等于或大于第二个参数,则返回小于、等于或大于零的整数。
返回值:返回一个数组类型值,该值包含所有其他数组中出现的第一个数组。如果没有匹配,数组返回NULL。
注意: 这是内置函数(array_uintersect_assoc()),用于比较数组的键和用户定义函数,用于比较值。
例子1
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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 comman in all array. |
例子2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 = Sofware, and Testing. |
让我们举一个简单的例子来理解array_uintersect_assoc()函数
程序1
取两个数组(array1和array2)并使用用户定义的键比较函数(arr_uintersectFunction)。
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 | <?php //PHP code for array_uintersect_assoc function function arr_uintersectFunction($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_uintersectFunction"); //print result. print_r($result); ?> |
输出
1 2 3 4 5 | Array ( [a] => Java [d] => in ) |
程序2
取两个数组(array1和array2)并使用用户定义的键比较函数(arr_uintersectFunction)。它没有匹配任何键和值,那么数组retrun NULL。
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 | <?php //PHP code for array_uintersect_assoc function //User-defined function function arr_uintersectFunction($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_uintersectFunction"); //print result. print_r($result); ?> |
输出
1 2 3 | Array ( ) |
程序3
返回所有参数中出现的$arr1的所有值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?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")); ?> |
输出
1 2 3 4 | Array ( [a] => gfg ) |