數組的交集 array_intersect()
array_intersect()函數返回一個保留了鍵的數組,這個數組只由第一個數組中出現的且在其他每個輸入數組中都出現的值組成。其形式如下:
1
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()基本相同,只不過他在比較中還考慮了數組的鍵。因此,只有在第一個數組中出現,且在所有其他輸入數組中也出現的鍵/值對才返回到結果數組中。
形式如下:
1
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()相反。
1
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()基本相同,只是它在比較時還考慮了數組的鍵。因此,只在第一個數組中出現而不再其他輸入數組中出現的鍵/值對才會返回到結果數組中。其形式如下:
1
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 )
?>
```
- 前端
- 技巧匯總
- 構建 Node + Webpack + React 熱加載開發環境
- React
- Redux
- Webpack
- ES6
- HTML5+CSS3
- Javascript
- JS超簡潔拖動代碼
- jQuery
- 后端
- 技巧匯總
- 代碼中特殊的注釋技術——TODO、FIXME和XXX的用處
- PHP
- Xdebug不解之謎
- PHP時間獲取
- PHP遞歸優化 使用匿名函數進行遞歸
- PHP 發起POST請求
- PHP獲得數組的交集與差集
- PHP遞歸獲取下級數組,可指定ID,一維數組
- PHP 判斷是否為Get/Post/Ajax提交
- PHP實現分流隊列平均顯示信息
- PHP多維數組 指定列排序
- PHP 類Class詳解 筆記記錄
- PHP取整函數詳解
- Node
- Elasticsearch
- 數據庫
- 技巧匯總
- Mysql
- Mysql分區表實現
- Mysql union與union all 查詢
- Mysql 表中表查詢
- Mysql 分組查詢 與 分組條件查詢
- MySQL 添加列,修改列,刪除列
- Mysql優化之:構建海量表,定位慢查詢
- Mysql優化之:表的設計滿足3NF
- Mysql優化常見方法
- Mysql存儲過程詳解
- 運維
- 技巧匯總
- Linux
- Linux Centos系統下 設置代理服務器上網
- Centos7增加開機啟動腳本
- centos 掛載windows共享目錄
- CentOS設置SSH Key登錄
- Linux/CentOS單網卡綁定多個IP
- Windows
- Win10開機啟動項設置全解攻略
- PuTTY連接Linux服務器經常斷線解決方案
- Docker
- Docke啟動文件 docker-compose.yml
- Docker命令簡介(未完)
- Docker閑雜筆記
- Apache
- Nginx
- Nginx配置upstream實現負載均衡
- Nginx負載均衡學習
- IDE
- 技巧匯總
- WebStorm
- PHPStorm
- 協作
- 技巧匯總
- Git
- git 放棄本地修改 強制更新
- git編譯安裝與常用命令
- Svn