1. 合併數組
array_merge()函數將數組合併到一塊兒,返回一個聯合的數組。所獲得的數組以第一個輸入數組參數開始,按後面數組參數出現的順序依次迫加。其形式爲:php
array array_merge (array array1 array2…,arrayN)
這個函數將一個或多個數組的單元合併起來,一個數組中的值附加在前一個數組的後面。返回做爲結果的數組。
若是輸入的數組中有相同的字符串鍵名,則該鍵名後面的值將覆蓋前一個值。然而,若是數組包含數字鍵名,後面的值將不會覆蓋原來的值,而是附加到後面。
若是隻給了一個數組而且該數組是數字索引的,則鍵名會以連續方式從新索引。
java
<?php $fruits = array("apple","banana","pear"); $numbered = array("1","2","3"); $cards = array_merge($fruits, $numbered); print_r($cards); // output // Array ( [0] => apple [1] => banana [2] => pear [3] => 1 [4] => 2 [5] => 3 ) ?>
2. 追加數組
array_merge_recursive()函數與array_merge()相同,能夠將兩個或多個數組合並在一塊兒,造成一個聯合的數組.二者之間的區別在於,當某個輸入數組中的某個鍵己經存在於結果數組中時該函數會採起不一樣的處理方式.array_merge()會覆蓋前面存在的鍵/值對,替換爲當前輸入數組中的鍵/值對,而array_merge_recursive()將把兩個值合併在一塊兒,造成一個新的數組,並以原有的鍵做爲數組名。還有一個數組合並的形式,就是遞歸追加數組。其形式爲:
array array_merge_recursive(array array1,array array2[…,array arrayN])
程序實例以下:
<?php
$fruit1 = array("apple" => "red", "banana" => "yellow");
$fruit2 = array("pear" => "yellow", "apple" => "green");
$result = array_merge_recursive($fruit1, $fruit2);
print_r($result);
// output
// Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow )
?>
如今鍵 apple 指向一個數組,這個數組由兩個顏色值組成的索引數組。
3. 鏈接數組
array_combine()函數會獲得一個新數組,它由一組提交的鍵和對應的值組成。其形式爲:
array array_combine(array keys,array values)
注意,兩個輸入數組必須大小相同,不能爲空。示例以下
<?php
$name = array("apple", "banana", "orange");
$color = array("red", "yellow", "orange");
$fruit = array_combine($name, $color);
print_r($fruit);
// output
// Array ( [apple] => red [banana] => yellow [orange] => orange )
?>
4. 拆分數組 array_slice()
array_slice()函數將返回數組中的一部分,從鍵offset開始,到offset+length位置結束。其形式:
array array_slice (array array, int offset[,int length])
offset 爲正值時,拆分將從距數組開頭的offset 位置開始;若是offset 爲負值,則拆分從距數組末尾的offset 位置開始。若是省略了可選參數length,則拆分將從offset 開始,一直到數組的最後一個元素。若是給出了length 且爲正數,則會在距數組開頭的offset+length 位置結束。相反,若是給出了length且爲負數,則在距數組開頭的count(input_array)-|length|位置結束。考慮一個例子:
<?php
$fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon");
$subset = array_slice($fruits, 3);
print_r($subset);
// output
// Array ( [0] => Pear [1] => Grape [2] => Lemon [3] => Watermelon )
?>
而後咱們使用下負長度:
<?php
$fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon");
$subset = array_slice($fruits, 2, -2);
print_r($subset);
// output
// Array ( [0] => Orange [1] => Pear [2] => Grape )
?>
5. 接合數組 array_splice()
array_splice()函數會刪除數組中從offset開始到offset+length 結束的全部元素,並以數組的形式返回所刪除的元素。其形式爲:
array array_splice ( array array , int offset[,length[,array replacement]])
offset 爲正值時,則接合將從距數組開頭的offset 位置開始,offset 爲負值時,接合將從距數組末尾的offset 位置開始。若是忽略可選的length 參數,則從offset 位置開始到數組結束之間的全部元素都將被刪除。若是給出了length 且爲正值,則接合將在距數組開頭的offset + leng th 位置結束。相反,若是給出了length且爲負值,則結合將在距數組開頭的count(input_array)-length的位置結束。實例以下:
<?php
$fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon");
$subset = array_splice($fruits, 4);
print_r($fruits);
print_r($subset);
// output
// Array ( [0] => Apple [1] => Banana [2] => Orange [3] => Pear )
// Array ( [0] => Grape [1] => Lemon [2] => Watermelon )
?>
可使用可選參數replacement來指定取代目標部分的數組。實例以下:
<?php
$fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon");
$subset = array_splice($fruits, 2, -1, array("Green Apple", "Red Apple"));
print_r($fruits);
print_r($subset);
// output
// Array ( [0] => Apple [1] => Banana [2] => Green Apple [3] => Red Apple [4] => Watermelon )
// Array ( [0] => Orange [1] => Pear [2] => Grape [3] => Lemon )
?>
從程序能夠很清楚看到這個函數的使用方法了。
6. 數組的交集 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()函數纔會認爲它們是相同的。
7. 關聯數組的交集 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 )
?>
8. 數組的差集 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 )
?>
9. 關聯數組的差集 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爲此提供了一些函數來知足需求。許多函數能完成兩項任務,不只能獲取當前指針位置的鍵或值,還能將指針移向下一個適當的位置。
10. 獲取當前數組鍵 key()
key()函數返回input_array中當前指針所在位置的鍵。其形式以下:
......
數組
轉載:http://justcoding.iteye.com/blog/1181962/app