array_uintersect()是PHP中的內置函數,用於根據值計算兩個或多個數組的交集。在用戶定義的函數的幫助下,將第一個數組值與所有其他數組進行比較,並返回匹配項。
用法:
array_uintersect($array1, $array2, $array3, ..... $arrayn, user_function
參數:該函數接受兩種類型的參數。一個是數組列表,另一個是用戶定義的函數。
- 數組清單:此函數接受以空格分隔的數組列表,我們要為其找到交集。在以上語法中,數組列表為$array1,$array2,$array3….. $arrayn。它可以接受任意數量的以空格分隔的數組,最小為2。
- user_function:這是一個字符串類型參數,它是用戶定義函數的名稱。當函數的參數值相同時,該函數返回0;如果第一個參數大於第二個參數,則返回1;否則返回-1。
返回值:該函數返回另一個數組,該數組包含在作為參數傳遞的所有其他數組中存在的第一個數組的所有元素。如果沒有元素匹配,則返回NULL數組。
例子:
Input : $a1=array("a"=>"striver", "b"=>"geeks", "d"=>"raj") $a2=array("d"=>"articles", "e"=>"raj", "f"=>"coding") Output : Array ( [d] => raj ) Input :$a1 = array("1"=>"geeks", "2"=>"for", "3"=>"geek", "4"=>"coding") $a2 = array("1"=>"geeks", "2"=>"for", "3"=>"php", "4"=>"coding", "5"=>"ide") $a3 = array("6"=>"cpp", "7"=>"java", 8=>"geeks") Output : Array ( [1] => geeks )
以下示例程序旨在說明array_uintersect()函數:
程序1:PHP程序演示array_uintersect()函數的工作。
<?php
// PHP program to demonstrate the working of
// array_uintersect() function
// user-defined function
function user_function($a, $b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
// arrays
$a1=array("a"=>"striver", "b"=>"geeks", "d"=>"raj");
$a2=array("d"=>"articles", "e"=>"raj", "f"=>"coding");
$result=array_uintersect($a1, $a2, "user_function");
print_r($result);
?>
輸出:
Array ( [d] => raj )
程序2:PHP程序演示具有三個數組的array_uintersect()函數的工作原理。
<?php
// PHP program to demonstrate the working of
// array_uintersect() function with 3 arrays
// user-defined function
function user_function($a, $b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
// 3 arrays
$a1 = array("1"=>"geeks", "2"=>"for", "3"=>"geek",
"4"=>"coding");
$a2 = array("1"=>"geeks", "2"=>"for", "3"=>"php",
"4"=>"coding", "5"=>"ide");
$a3 = array("6"=>"cpp", "7"=>"java", 8=>"geeks");
$result=array_uintersect($a1, $a2, $a3, "user_function");
print_r($result);
?>
輸出:
Array ( [1] => geeks )
參考:
http://php.net/manual/en/function.array-uintersect.php
相關用法
- p5.js nfc()用法及代碼示例
- p5.js nfp()用法及代碼示例
- d3.js d3.hcl()用法及代碼示例
- p5.js nfs()用法及代碼示例
- PHP cos( )用法及代碼示例
- PHP sin( )用法及代碼示例
- p5.js nf()用法及代碼示例
- PHP tan( )用法及代碼示例
- PHP pow( )用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- d3.js d3.set.has()用法及代碼示例
- PHP Ds\Set xor()用法及代碼示例
注:本文由純淨天空篩選整理自Striver大神的英文原創作品 PHP | array_uintersect() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。