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


PHP array_uintersect_assoc()用法及代碼示例


array_uintersect_assoc()函數是PHP中的內置函數,用於為兩個或多個數組的不同值計算鍵數組的交集。初始數組或第一個數組通過回調函數或用戶定義的函數與所有其他數組進行比較,並返回匹配項。與array_uintersect()函數不同,鍵用於比較。

用法:

array array_uintersect_assoc( $array1, $array2, $array3... array nth, arr_uintersect_Function)

參數:該函數接受上述和以下描述的許多參數:


  • $array1:它是要與另一個數組進行比較的初始數組。它是必填參數。
  • $array2:與第一個數組鍵相比,它是第二個數組。它也是必填參數。
  • $array3…:將數組與第一個數組鍵進行比較。它是可選參數。
  • arr_uintersect_Function:它是必需的參數,用於保存用戶定義的函數。它是一個字符串,用於定義用戶定義的回調函數,並且如果第一個參數小於,等於或大於第二個參數,則返回小於,等於或大於零的整數。

返回值:它返回一個數組類型值,其中包含所有其他數組中存在的第一個數組。如果沒有匹配項,則返回NULL。

注意:array_uintersect_assoc()函數比較數組的鍵,而用戶定義的函數比較值。

例子:

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 common in all arrays.


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 = Software, and Testing.

以下示例程序旨在說明PHP中的array_uintersect_assoc()函數:

程序1:該程序使用兩個數組(array1和array2)和一個用戶定義的鍵比較函數(arr_uintersect_Function)。

<?php 
  
// PHP program for array_uintersect_assoc() function 
function arr_uintersect_Function($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_uintersect_Function"); 
  
// Display result 
print_r($result); 
  
?>
輸出:
Array
(
    [a] => Java
    [d] => in
)

程序2:該程序使用兩個數組(array1和array2)和一個用戶定義的鍵比較函數(arr_uintersect_Function)。如果數組與任何鍵和值都不匹配,則返回NULL。

<?php 
  
// PHP program for array_uintersect_assoc() function 
  
// User-defined function 
function arr_uintersect_Function($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_uintersect_Function" ); 
  
// Display result 
print_r($result); 
  
?>
輸出:
Array
(
)

程序3:該程序返回所有參數中存在的$arr1的所有值。

<?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") ); 
  
?>
輸出:
Array
(
    [a] => gfg
)

參考: https://www.php.net/manual/en/function.array-uintersect-assoc.php



相關用法


注:本文由純淨天空篩選整理自jit_t大神的英文原創作品 PHP | array_uintersect_assoc() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。