array_intersect_ ukey() Function
函数的作用是:在用户定义函数的帮助下,将两个或多个数组与键进行比较,该函数返回一个数组。返回的数组是第一个数组,它匹配键,所有参数中显示的是最终输出。
语法
1 | array_intersect_ ukey($array1, $array2, $array3……., ukey_intersectFunction) |
参数:该函数接受两个参数,第一个参数是数组列表($array1, $array2, $array3,……$arraynth)和最少两个数组。第二个参数是用户定义的函数(ukey_intersectFunction),参数描述如下:
- array1:它是初始数组,并与另一个数组进行比较。这是强制性的。
- array2:与第一个数组键相比较的数组。这是强制性的。
- array3:第二个数组与第一个数组键的比较。它是可选的。
- ukey_intersectFunction:需要用户定义的函数。定义可调用比较函数的字符串。
返回值:返回数组类型值,该值包含第一个数组,其键在所有其他参数中退出。
注意:该函数使用用户定义的函数来比较键。不要比较键值。
让我们举一些例子来理解array_intersect_ ukey()函数。
程序1
取两个数组(array1和array2),并使用用户定义的键比较函数(ukey_intersectFunction)。
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 | <?php // PHP array_intersect_ ukey() function // program function ukey_intersectFunction($x, $y) { if ($x === $y) return 0; return ($x > $y)? 1: -1; } $a1 = array( "a" => "Java", "b" => "C++", "c" => "XML", "d" => "Python", "p" => "PHP" ); $a2 = array( "a" => "Python", "b" => "CSS", "h" => "VB", "x" => "HTML", "p" => "SQL" ); // print result wich key is matching form both // array and print first arary elements. $result = array_intersect_ukey($a1, $a2, "ukey_intersectFunction"); print_r($result); ?> |
输出
1 2 3 4 5 6 | Array ( [a] => Java [b] => C++ [p] => PHP ) |
程序2
取4个数组(array1、array2、array3、array4),使用用户定义的键比较函数(ukey_intersectFunction)。
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 40 41 42 43 44 45 46 47 48 | <?php // PHP array_intersect_ ukey() function // program function ukey_intersectFunction($x, $y) { if ($x === $y) return 0; return ($x > $y)? 1: -1; } // Arrays $arr1 = array( "a" => "A", "b" => "B", "c" => "D", "d" => "E", "p" => "F" ); $arr2 = array( "o" => "A", "b" => "B", "h" => "V", "x" => "T", "p" => "X" ); $arr3 = array( "a" => "N", "b" => "B", "h" => "O", "x" => "T", "p" => "P" ); $arr4 = array( "s" => "W", "b" => "B", "z" => "D", "x" => "T", "p" => "P" ); // print result wich key is matching form both // array and print first arary elements. $result = array_intersect_ukey($arr1, $arr2, $arr3, $arr4, "ukey_intersectFunction"); print_r($result); ?> |
输出
1 2 3 4 5 | Array ( [b] => B [p] => F ) |
程序3
以三个数组(array1、array2和array3)为例,使用用户定义的键比较函数,它们都具有相同的键和不同的值。返回并显示array1的结果。
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 array_intersect_ ukey() function // program function ukey_intersectFunction($x, $y) { if ($x === $y) return 0; return ($x > $y)? 1: -1; } // Arrays $arr1 = array( "a" => "Bhopal", "b" => "New Delhi", "n" => "Madras", "o" => "Mumbai", "p" => "Finland" ); $arr2 = array( "a" => "Bhubaneswar", "b" => "Ranchi", "n" => "Kolkatta", "o" => "Luknow", "p" => "Allahabad" ); $arr3 = array( "a" => "Rourkela", "b" => "Jammu", "n" => "Kasmir", "o" => "Tripura", "p" => "Sillong" ); // print result wich key is matching form both // array and print first arary elements. $result = array_intersect_ukey($arr1, $arr2, $arr3, "ukey_intersectFunction"); print_r($result); ?> |
输出
1 2 3 4 5 6 7 8 | Array ( [a] => Bhopal [b] => New Delhi [n] => Madras [o] => Mumbai [p] => Finland ) |
程序4
取两个数组(array1和array2),使用用户定义的键比较函数(ukey_intersectFunction)。两个数组都有不同的键,那么数组返回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 | <?php // PHP array_intersect_ ukey() function // program function ukey_intersectFunction($x, $y) { if ($x === $y) return 0; return ($x > $y)? 1: -1; } // Arrays $arr1 = array( "a" => "IIT Sillong", "b" => "IIT Bhopal", "n" => "NIT Bhopal" ); $arr2 = array( "x" => "IIT Bhubaneswar", "y" => "IIT Ranchi", "z" => "NIT Durgapur" ); // print result wich key is matching form both // array and print first arary elements. $result = array_intersect_ukey($arr1, $arr2, "ukey_intersectFunction"); print_r($result); ?> |
输出
1 2 3 | Array ( ) |
程序5
以两个数组(array1和array2)为例,两个数组的键不同,但索引相同,比较数组返回NULL/Empty数组。
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 | <?php // PHP array_intersect_ ukey() function // program function ukey_intersectFunction($x, $y) { if ($x === $y) return 0; return ($x > $y)? 1: -1; } // Arrays $arr1 = array( "a" => "IIT", "b" => "CIT", "n" => "NIT" ); $arr2 = array( "x" => "IIT", "y" => "CIT", "z" => "NIT" ); // print result wich key is matching form both // array and print first arary elements. $result = array_intersect_ukey($arr1, $arr2, "ukey_intersectFunction"); print_r($result); ?> |
输出
1 2 3 | Array ( ) |
参考