当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP array_intersect_ukey()用法及代码示例


array_intersect_ukey()函数是PHP的内置函数,该函数用于在用户定义函数的帮助下针对键计算两个或多个数组的交集,并且该函数返回数组。返回的数组是第一个数组,它是匹配键,并且所有参数中都存在的是最终输出。

用法:

array array_intersect_ukey( $array1, $array2, ..., $ukey_intersectFunction )

参数:该函数接受两个或多个数组以及一个$ukey_intersectFunction参数,如上所述并在下面进行了描述:


  • $array1:它是用于与其他数组进行比较的初始数组。它是必填参数。
  • $array2:该数组与第一个数组键进行比较。它是必填参数。
  • $array。 。 :与第一个数组键进行比较的数组参数列表。它是可选参数。
  • $ukey_intersectFunction:这是保留用户定义函数的必需参数。定义可调用比较函数的字符串。

返回值:它返回一个包含第一个数组的值的数组,该数组的键存在于所有其他参数中。

注意:此函数使用用户定义的函数比较键。不比较键的值。

以下示例说明了PHP中的array_intersect_ukey()函数:

程序1:该程序使用两个数组(array1和array2)和一个用户定义的键比较函数(ukey_intersectFunction)来计算数组的交集。

<?php 
  
// PHP program to illustrates the 
// array_intersect_ukey() function 
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 results if the key is matching form both 
// array and print first array elements. 
$result = array_intersect_ukey($a1, $a2, "ukey_intersectFunction"); 
print_r($result); 
?>
输出:
Array
(
    [a] => Java
    [b] => C++
    [p] => PHP
)

程序2:该程序使用四个数组(array1,array2,array3和array4)和一个用户定义的键比较函数(ukey_intersectFunction)计算数组的交集。

<?php 
  
// PHP program to illustrates the 
// array_intersect_ukey() function 
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 the result if key is matching form both 
// array and print first array elements. 
$result = array_intersect_ukey($arr1, 
   $arr2, $arr3, $arr4, "ukey_intersectFunction"); 
  
print_r($result); 
  
?>
输出:
Array
(
    [b] => B
    [p] => F
)

程序3:该程序使用三个数组(array1,array2和array3)和一个用户定义的键比较函数来计算数组的交集。

<?php 
  
// PHP program to illustrates the 
// array_intersect_ukey() function 
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 the result if key is matching form both 
// array and print first array elements. 
$result = array_intersect_ukey($arr1, 
      $arr2, $arr3, "ukey_intersectFunction"); 
print_r($result); 
?>
输出:
Array
(
    [a] => Bhopal
    [b] => New Delhi
    [n] => Madras
    [o] => Mumbai
    [p] => Finland
)

程序4:该程序使用两个数组(array1和array2)以及一个用户定义的键比较函数(ukey_intersectFunction)来计算数组的交集。两个数组都有不同的键,然后数组返回NULL /Empty Array。


<?php 
  
// PHP program to illustrates the 
// array_intersect_ukey() function 
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 the result if key is matching form both 
// array and print first array elements. 
$result = array_intersect_ukey($arr1, 
         $arr2, "ukey_intersectFunction"); 
print_r($result); 
?>
输出:
Array
(
)

程序5:该程序使用两个数组(array1和array2),并且两个数组具有不同的键,但在同一索引中的值相同。比较数组并返回NULL /Empty数组。

<?php 
  
// PHP program to illustrates the 
// array_intersect_ukey() function 
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 the result if key is matching form both 
// array and print first array elements. 
$result = array_intersect_ukey($arr1, 
         $arr2, "ukey_intersectFunction"); 
  
print_r($result); 
?>
输出:
Array
(
)

相关文章:

参考: http://php.net/manual/en/function.array-intersect-ukey.php



相关用法


注:本文由纯净天空筛选整理自jit_t大神的英文原创作品 PHP | array_intersect_ukey() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。