PHP獲得數組交集與差集的方法,php獲得數組交集_PHP教程

來源:互聯網
上載者:User

PHP獲得數組交集與差集的方法,php獲得數組交集


本文執行個體講述了PHP獲得數組交集與差集的方法。分享給大家供大家參考。具體分析如下:

一、數組的交集 array_intersect()

array_intersect()函數返回一個保留了鍵的數組,這個數組只由第一個數組中出現的且在其他每個輸入數組中都出現的值組成。其形式如下:

array array_intersect(array array1,array array2[,arrayN…])

下面這個例子將返回在$fruit1數組中出現的且在$fruit2和$fruit3中也出現的所有的水果:

<?php$fruit1 = array("Apple","Banana","Orange");$fruit2 = array("Pear","Apple","Grape");$fruit3 = array("Watermelon","Orange","Apple");$intersection = array_intersect($fruit1, $fruit2, $fruit3);print_r($intersection);// output// Array ( [0] => Apple )?>

只有在兩個元素相等且具有相同的資料類型時,array_intersect()函數才會認為它們是相同的。

關聯陣列的交集 array_intersect_assoc()

函數array_intersect_assoc()與array_intersect()基本相同,只不過他在比較中還考慮了數組的鍵。因此,只有在第一個數組中出現,且在所有其他輸入數組中也出現的鍵/值對才返回到結果數組中。

形式如下:

array array_intersect_assoc(array array1,array array2[,arrayN…])

下面的例子返回了出現在$fruit1數組中,也同時出現在$fruit2與$fruit3中的所有鍵/值對:

<?php$fruit1 = array("red"=>"Apple","yellow"=>"Banana","orange"=>"Orange");$fruit2 = array("yellow"=>"Pear","red"=>"Apple","purple"=>"Grape");$fruit3 = array("green"=>"Watermelon","orange"=>"Orange","red"=>"Apple");$intersection = array_intersect_assoc($fruit1, $fruit2, $fruit3);print_r($intersection);// output// Array ( [red] => Apple )?>

二、數組的差集 array_diff()

函數array_diff()返回出現在第一個數組中但其他輸入數組中沒有的值。這個功能與array_intersect()相反。

array array_diff(array array1,array array2[,arrayN…])

執行個體如下:

<?php$fruit1 = array("Apple","Banana","Orange");$fruit2 = array("Pear","Apple","Grape");$fruit3 = array("Watermelon","Orange","Apple");$intersection = array_diff($fruit1, $fruit2, $fruit3);print_r($intersection);// output// Array ( [1] => Banana )?>

關聯陣列的差集 array_diff_assoc()

函數array_diff_assoc()與array_diff()基本相同,只是它在比較時還考慮了數組的鍵。因此,只在第一個數組中出現而不再其他輸入數組中出現的鍵/值對才會返回到結果數組中。其形式如下:

array array_diff_assoc(array array1,array array2[,arrayN…])

下面的例子只返回了[yellow] => Banana,因為這個特殊的鍵/值對出現在$fruit1中,而在$fruit2和$fruit3中都不存在。

<?php$fruit1 = array("red"=>"Apple","yellow"=>"Banana","orange"=>"Orange");$fruit2 = array("yellow"=>"Pear","red"=>"Apple","purple"=>"Grape");$fruit3 = array("green"=>"Watermelon","orange"=>"Orange","red"=>"Apple");$intersection = array_diff_assoc($fruit1, $fruit2, $fruit3);print_r($intersection);// output// Array ( [yellow] => Banana )?>

希望本文所述對大家的php程式設計有所協助。

http://www.bkjia.com/PHPjc/1014424.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1014424.htmlTechArticlePHP獲得數組交集與差集的方法,php獲得數組交集 本文執行個體講述了PHP獲得數組交集與差集的方法。分享給大家供大家參考。具體分析如下:...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.