在PHP中,數(shù)組運(yùn)算符是用于對(duì)數(shù)組進(jìn)行操作的特殊運(yùn)算符,PHP數(shù)組運(yùn)算符包括聯(lián)合、相等、全等、不等、不全等等,可以用于合并數(shù)組、比較數(shù)組,以及對(duì)數(shù)組進(jìn)行其他的操作,使代碼更加簡(jiǎn)潔和易讀,同時(shí)也可以提高代碼的效率。
一、數(shù)組運(yùn)算符
例子 | 名稱 | 結(jié)果 |
$a + $b | 聯(lián)合 | $a 和 $b 的聯(lián)合。 |
$a == $b | 相等 | 如果 $a 和 $b 具有相同的鍵/值對(duì)則為 true。 |
$a === $b | 全等 | 如果 $a 和 $b 具有相同的鍵/值對(duì)并且順序和類型都相同則為 true。 |
$a != $b | 不等 | 如果 $a 不等于 $b 則為 true。 |
$a <> $b | 不等 | 如果 $a 不等于 $b 則為 true。 |
$a !== $b | 不全等 | 如果 $a 不全等于 $b 則為 true。 |
+ 運(yùn)算符把右邊的數(shù)組元素附加到左邊的數(shù)組后面,兩個(gè)數(shù)組中都有的鍵名,則只用左邊數(shù)組中的,右邊的被忽略。
<?php $a = array("a" => "apple", "b" => "banana"); $b = array("a" => "pear", "b" => "strawberry", "c" => "cherry"); $c = $a + $b; // $a 和 $b 的并集 echo "Union of \$a and \$b: \n"; var_dump($c); $c = $b + $a; // $b 和 $a 的并集 echo "Union of \$b and \$a: \n"; var_dump($c); $a += $b; // $a += $b 的并集是 $a 和 $b echo "Union of \$a += \$b: \n"; var_dump($a); ?>
執(zhí)行后,此腳本會(huì)顯示:
Union of $a and $b: array(3) { ["a"]=> string(5) "apple" ["b"]=> string(6) "banana" ["c"]=> string(6) "cherry" } Union of $b and $a: array(3) { ["a"]=> string(4) "pear" ["b"]=> string(10) "strawberry" ["c"]=> string(6) "cherry" } Union of $a += $b: array(3) { 'a' => string(5) "apple" 'b' => string(6) "banana" 'c' => string(6) "cherry" }
數(shù)組中的單元如果具有相同的鍵名和值則比較時(shí)相等。
示例 : 比較數(shù)組
<?php $a = array("apple", "banana"); $b = array(1 => "banana", "0" => "apple"); var_dump($a == $b); // bool(true) var_dump($a === $b); // bool(false) ?>
二、數(shù)組函數(shù)
- array_change_key_case : 將數(shù)組中的所有鍵名修改為全大寫或小寫;
- array_chunk : 將一個(gè)數(shù)組分割成多個(gè);
- array_column : 返回輸入數(shù)組中指定列的值;
- array_combine : 創(chuàng)建一個(gè)數(shù)組,用一個(gè)數(shù)組的值作為其鍵名,另一個(gè)數(shù)組的值作為其值;
- array_count_values : 統(tǒng)計(jì)數(shù)組中每個(gè)不同值的出現(xiàn)次數(shù);
- array_diff_assoc : 帶索引檢查計(jì)算數(shù)組的差集;
- array_diff_key : 使用鍵名比較計(jì)算數(shù)組的差集;
- array_diff_uassoc : 用用戶提供的回調(diào)函數(shù)做索引檢查來(lái)計(jì)算數(shù)組的差集;
- array_diff_ukey : 用回調(diào)函數(shù)對(duì)鍵名比較計(jì)算數(shù)組的差集;
- array_diff : 計(jì)算數(shù)組的差集;
- array_fill_keys : 使用指定的鍵和值填充數(shù)組;
- array_fill : 用給定的值填充數(shù)組;
- array_filter : 使用回調(diào)函數(shù)過(guò)濾數(shù)組的元素;
- array_flip : 交換數(shù)組中的鍵和值;
- array_intersect_assoc : 帶索引檢查計(jì)算數(shù)組的交集;
- array_intersect_key : 使用鍵名比較計(jì)算數(shù)組的交集;
- array_intersect_uassoc : 帶索引檢查計(jì)算數(shù)組的交集,用回調(diào)函數(shù)比較索引;
- array_intersect_ukey : 在鍵名上使用回調(diào)函數(shù)來(lái)比較計(jì)算數(shù)組的交集;
- array_intersect : 計(jì)算數(shù)組的交集;
- array_is_list : 判斷給定的 array 是否為 list;
- array_key_exists : 檢查數(shù)組里是否有指定的鍵名或索引;
- array_key_first : 獲取指定數(shù)組的第一個(gè)鍵;
- array_key_last : 獲取一個(gè)數(shù)組的最后一個(gè)鍵值;
- array_keys : 返回?cái)?shù)組中部分的或所有的鍵名;
- array_map : 為數(shù)組的每個(gè)元素應(yīng)用回調(diào)函數(shù);
- array_merge_recursive : 遞歸地合并一個(gè)或多個(gè)數(shù)組;
- array_merge : 合并一個(gè)或多個(gè)數(shù)組;
- array_multisort : 對(duì)多個(gè)數(shù)組或多維數(shù)組進(jìn)行排序;
- array_pad : 以指定長(zhǎng)度將一個(gè)值填充進(jìn)數(shù)組;
- array_pop : 彈出數(shù)組最后一個(gè)單元(出棧);
- array_product : 計(jì)算數(shù)組中所有值的乘積;
- array_push : 將一個(gè)或多個(gè)單元壓入數(shù)組的末尾(入棧);
- array_rand : 從數(shù)組中隨機(jī)取出一個(gè)或多個(gè)隨機(jī)鍵;
- array_reduce : 用回調(diào)函數(shù)迭代地將數(shù)組簡(jiǎn)化為單一的值;
- array_replace_recursive : 使用傳遞的數(shù)組遞歸替換第一個(gè)數(shù)組的元素;
- array_replace : 使用傳遞的數(shù)組替換第一個(gè)數(shù)組的元素;
- array_reverse : 返回單元順序相反的數(shù)組;
- array_search : 在數(shù)組中搜索給定的值,如果成功則返回首個(gè)相應(yīng)的鍵名;
- array_shift : 將數(shù)組開頭的單元移出數(shù)組;
- array_slice : 從數(shù)組中取出一段;
- array_splice : 去掉數(shù)組中的某一部分并用其它值取代;
- array_sum : 對(duì)數(shù)組中所有值求和;
- array_udiff_assoc : 帶索引檢查計(jì)算數(shù)組的差集,用回調(diào)函數(shù)比較數(shù)據(jù);
- array_udiff_uassoc : 帶索引檢查計(jì)算數(shù)組的差集,用回調(diào)函數(shù)比較數(shù)據(jù)和索引;
- array_udiff : 用回調(diào)函數(shù)比較數(shù)據(jù)來(lái)計(jì)算數(shù)組的差集;
- array_uintersect_assoc : 帶索引檢查計(jì)算數(shù)組的交集,用回調(diào)函數(shù)比較數(shù)據(jù);
- array_uintersect_uassoc : 帶索引檢查計(jì)算數(shù)組的交集,用單獨(dú)的回調(diào)函數(shù)比較數(shù)據(jù)和索引;
- array_uintersect : 計(jì)算數(shù)組的交集,用回調(diào)函數(shù)比較數(shù)據(jù);
- array_unique : 移除數(shù)組中重復(fù)的值;
- array_unshift : 在數(shù)組開頭插入一個(gè)或多個(gè)單元;
- array_values : 返回?cái)?shù)組中所有的值;
- array_walk_recursive : 對(duì)數(shù)組中的每個(gè)成員遞歸地應(yīng)用用戶函數(shù);
- array_walk : 使用用戶自定義函數(shù)對(duì)數(shù)組中的每個(gè)元素做回調(diào)處理;
- array : 新建一個(gè)數(shù)組;
- arsort : 對(duì)數(shù)組進(jìn)行降向排序并保持索引關(guān)系;
- asort : 對(duì)數(shù)組進(jìn)行升序排序并保持索引關(guān)系;
- compact : 建立一個(gè)數(shù)組,包括變量名和它們的值;
- count : 統(tǒng)計(jì)數(shù)組、Countable 對(duì)象中所有元素的數(shù)量;
- current : 返回?cái)?shù)組中的當(dāng)前值;
- each : 返回?cái)?shù)組中當(dāng)前的鍵/值對(duì)并將數(shù)組指針向前移動(dòng)一步;
- end : 將數(shù)組的內(nèi)部指針指向最后一個(gè)單元;
- extract : 從數(shù)組中將變量導(dǎo)入到當(dāng)前的符號(hào)表;
in_array : 檢查數(shù)組中是否存在某個(gè)值; - key_exists : 別名 array_key_exists;
- key : 從關(guān)聯(lián)數(shù)組中取得鍵名;
- krsort : 對(duì)數(shù)組按照鍵名逆向排序;
- ksort : 對(duì)數(shù)組根據(jù)鍵名升序排序;
- list : 把數(shù)組中的值賦給一組變量;
- natcasesort : 用“自然排序”算法對(duì)數(shù)組進(jìn)行不區(qū)分大小寫字母的排序;
- natsort : 用“自然排序”算法對(duì)數(shù)組排序;
- next : 將數(shù)組中的內(nèi)部指針向前移動(dòng)一位;
- pos : current 的別名;
- prev : 將數(shù)組的內(nèi)部指針倒回一位;
- range : 根據(jù)范圍創(chuàng)建數(shù)組,包含指定的元素;
- reset : 將數(shù)組的內(nèi)部指針指向第一個(gè)單元;
- rsort : 對(duì)數(shù)組降序排序;
- shuffle : 打亂數(shù)組;
- sizeof : count 的別名;
- sort : 對(duì)數(shù)組升序排序;
- uasort : 使用用戶定義的比較函數(shù)對(duì)數(shù)組進(jìn)行排序并保持索引關(guān)聯(lián);
- uksort : 使用用戶自定義的比較函數(shù)對(duì)數(shù)組中的鍵名進(jìn)行排序;
- usort : 使用用戶自定義的比較函數(shù)對(duì)數(shù)組中的值進(jìn)行排序。