當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。