php中兩個數(shù)組求交集的函數(shù)是哪個

本篇內(nèi)容介紹了“php中兩個數(shù)組求交集的函數(shù)是哪個”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)來電聯(lián)系:028-86922220,為您提供成都網(wǎng)站建設(shè)網(wǎng)頁設(shè)計及定制高端網(wǎng)站建設(shè)服務(wù),創(chuàng)新互聯(lián)網(wǎng)頁制作領(lǐng)域10年,包括茶藝設(shè)計等多個方面擁有豐富的網(wǎng)站維護經(jīng)驗,選擇創(chuàng)新互聯(lián),為企業(yè)錦上添花!

有8個交集函數(shù):1、array_intersect(),只比較鍵值;2、array_intersect_assoc(),比較鍵名和鍵值;3、array_intersect_key(),只比較鍵名;4、array_uintersect()等。

php中兩個數(shù)組求交集的函數(shù)是哪個

本教程操作環(huán)境:windows7系統(tǒng)、PHP7.1版、DELL G3電腦

php中提供例如多個求數(shù)組交集的函數(shù):

  • array_intersect():比較數(shù)組,返回兩個數(shù)組的交集(只比較鍵值)。

  • array_intersect_assoc():比較數(shù)組,返回兩個數(shù)組的交集(比較鍵名和鍵值)。

  • array_intersect_key():比較數(shù)組,返回兩個數(shù)組的交集(只比較鍵名)。

  • array_intersect_uassoc():比較數(shù)組,返回兩個數(shù)組的交集(比較鍵名和鍵值,使用用戶自定義比較函數(shù))。

  • array_intersect_ukey():比較數(shù)組,返回兩個數(shù)組的交集(只比較鍵名,使用用戶自定義比較函數(shù))。

  • array_uintersect():比較數(shù)組,返回兩個數(shù)組的交集(只比較鍵值,使用一個用戶自定義比較函數(shù))。

  • array_uintersect_assoc():比較數(shù)組,返回兩個數(shù)組的交集(比較鍵名和鍵值,使用內(nèi)建函數(shù)比較,使用用戶自定義函數(shù)比較鍵值)。

  • array_uintersect_uassoc():比較數(shù)組,返回兩個數(shù)組的交集(比較鍵名和鍵值,使用兩個用戶自定義的比較函數(shù))。

下面介紹一下常用求數(shù)組交集的比較函數(shù)

1、array_intersect()函數(shù)

array_intersect() 函數(shù)用于比較兩個(或更多個)數(shù)組的值,并返回交集。

該函數(shù)比較兩個(或更多個)數(shù)組的值,并返回一個交集數(shù)組,該數(shù)組包含了所有在 array1 中也同時出現(xiàn)在所有其它參數(shù)數(shù)組中的值。

<?php
header('content-type:text/html;charset=utf-8');   
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
 
$result=array_intersect($a1,$a2);
var_dump($result);
?>

php中兩個數(shù)組求交集的函數(shù)是哪個

2、array_intersect_assoc()函數(shù)

array_intersect_assoc() 函數(shù)用于比較兩個(或更多個)數(shù)組的鍵名和鍵值,并返回交集。

該函數(shù)比較兩個(或更多個)數(shù)組的鍵名和鍵值,并返回一個交集數(shù)組,該數(shù)組包括了所有在被比較的數(shù)組(array1)中,同時也在任何其他參數(shù)數(shù)組(array2 或 array3 等等)中的鍵名和鍵值。

<?php
header('content-type:text/html;charset=utf-8');   
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","b"=>"green","c"=>"blue");

$result=array_intersect_assoc($a1,$a2);
var_dump($result);
?>

php中兩個數(shù)組求交集的函數(shù)是哪個

3、array_intersect_key()函數(shù)

<?php
header('content-type:text/html;charset=utf-8');   
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","c"=>"blue","d"=>"pink");

$result=array_intersect_key($a1,$a2);
var_dump($result);
?>

php中兩個數(shù)組求交集的函數(shù)是哪個

說明:不常用的比較函數(shù)

  • array_intersect_uassoc()

  • array_intersect_ukey()

  • array_uintersect()

  • array_uintersect_assoc()

  • array_uintersect_uassoc()

它們都使用用戶自定義函數(shù)來比較函數(shù)

例:

<?php
header('content-type:text/html;charset=utf-8');   
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}

$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("d"=>"red","b"=>"green","e"=>"blue");

$result=array_intersect_uassoc($a1,$a2,"myfunction");
var_dump($result);
?>

php中兩個數(shù)組求交集的函數(shù)是哪個

“php中兩個數(shù)組求交集的函數(shù)是哪個”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

文章題目:php中兩個數(shù)組求交集的函數(shù)是哪個
文章起源:http://bm7419.com/article38/gegdsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站導(dǎo)航自適應(yīng)網(wǎng)站、域名注冊、網(wǎng)站設(shè)計公司、手機網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計