教育行業(yè)A股IPO第一股(股票代碼 003032)

全國咨詢/投訴熱線:400-618-4000

PHP數(shù)組函數(shù)一網(wǎng)打盡

更新時(shí)間:2018年03月27日15時(shí)24分 來源:傳智播客 瀏覽次數(shù):

PHP數(shù)組函數(shù)一網(wǎng)打盡(合并,拆分,追加,查找,刪除等)

1. 合并數(shù)組

array_merge()函數(shù)將數(shù)組合并到一起,返回一個(gè)聯(lián)合的數(shù)組。所得到的數(shù)組以第一個(gè)輸入數(shù)組參數(shù)開始,按后面數(shù)組參數(shù)出現(xiàn)的順序依次迫加。這個(gè)函數(shù)將一個(gè)或多個(gè)數(shù)組的單元合并起來,一個(gè)數(shù)組中的值附加在前一個(gè)數(shù)組的后面。返回作為結(jié)果的數(shù)組。

如果輸入的數(shù)組中有相同的字符串鍵名,則該鍵名后面的值將覆蓋前一個(gè)值。然而,如果數(shù)組包含數(shù)字鍵名,后面的值將不會(huì)覆蓋原來的值,而是附加到后面。

如果只給了一個(gè)數(shù)組并且該數(shù)組是數(shù)字索引的,則鍵名會(huì)以連續(xù)方式重新索引。其形式為:array array_merge (array array1 array2…,arrayN)

案例如下:

$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 )

?>

PHP數(shù)組函數(shù)介紹

2. 追加數(shù)組

array_merge_recursive()函數(shù)與array_merge()相同,可以將兩個(gè)或多個(gè)數(shù)組合并在一起,形成一個(gè)聯(lián)合的數(shù)組.兩者之間的區(qū)別在于,當(dāng)某個(gè)輸入數(shù)組中的某個(gè)鍵己經(jīng)存在于結(jié)果數(shù)組中時(shí)該函數(shù)會(huì)采取不同的處理方式.array_merge()會(huì)覆蓋前面存在的鍵/值對,替換為當(dāng)前輸入數(shù)組中的鍵/值對,而array_merge_recursive()將把兩個(gè)值合并在一起,形成一個(gè)新的數(shù)組,并以原有的鍵作為數(shù)組名。還有一個(gè)數(shù)組合并的形式,就是遞歸追加數(shù)組。其形式為:array array_merge_recursive(array array1,array array2[…,array arrayN])

程序?qū)嵗缦拢?/p>

$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 )

?>

現(xiàn)在鍵 apple 指向一個(gè)數(shù)組,這個(gè)數(shù)組由兩個(gè)顏色值組成的索引數(shù)組。

3. 連接數(shù)組

array_combine()函數(shù)會(huì)得到一個(gè)新數(shù)組,它由一組提交的鍵和對應(yīng)的值組成。其形式為:

array array_combine(array keys,array values)

注意,兩個(gè)輸入數(shù)組必須大小相同,不能為空。示例如下

$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. 拆分?jǐn)?shù)組 array_slice()

array_slice()函數(shù)將返回?cái)?shù)組中的一部分,從鍵offset開始,到offset+length位置結(jié)束。其形式:

array array_slice (array array, int offset[,int length])

offset 為正值時(shí),拆分將從距數(shù)組開頭的offset 位置開始;如果offset 為負(fù)值,則拆分從距數(shù)組末尾的offset 位置開始。如果省略了可選參數(shù)length,則拆分將從offset 開始,一直到數(shù)組的最后一個(gè)元素。如果給出了length 且為正數(shù),則會(huì)在距數(shù)組開頭的offset+length 位置結(jié)束。相反,如果給出了length且為負(fù)數(shù),則在距數(shù)組開頭的count(input_array)-|length|位置結(jié)束??紤]一個(gè)例子:

$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 )

?>

然后我們使用下負(fù)長度:

$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. 接合數(shù)組 array_splice()

array_splice()函數(shù)會(huì)刪除數(shù)組中從offset開始到offset+length 結(jié)束的所有元素,并以數(shù)組的形式返回所刪除的元素。其形式為:

array array_splice ( array array , int offset[,length[,array replacement]])

offset 為正值時(shí),則接合將從距數(shù)組開頭的offset 位置開始,offset 為負(fù)值時(shí),接合將從距數(shù)組末尾的offset 位置開始。如果忽略可選的length 參數(shù),則從offset 位置開始到數(shù)組結(jié)束之間的所有元素都將被刪除。如果給出了length 且為正值,則接合將在距數(shù)組開頭的offset + leng th 位置結(jié)束。相反,如果給出了length且為負(fù)值,則結(jié)合將在距數(shù)組開頭的count(input_array)-length的位置結(jié)束。實(shí)例如下:

$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 )

?>

可以使用可選參數(shù)replacement來指定取代目標(biāo)部分的數(shù)組。實(shí)例如下:

$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 )

?>

從程序可以很清楚看到這個(gè)函數(shù)的使用方法了。

6. 數(shù)組的交集 array_intersect()

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

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

下面這個(gè)例子將返回在$fruit1數(shù)組中出現(xiàn)的且在$fruit2和$fruit3中也出現(xiàn)的所有的水果:

$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 )

?>

只有在兩個(gè)元素相等且具有相同的數(shù)據(jù)類型時(shí),array_intersect()函數(shù)才會(huì)認(rèn)為它們是相同的。

7. 關(guān)聯(lián)數(shù)組的交集 array_intersect_assoc()

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

形式如下:array array_intersect_assoc(array array1,array array2[,arrayN…])

下面的例子返回了出現(xiàn)在$fruit1數(shù)組中,也同時(shí)出現(xiàn)在$fruit2與$fruit3中的所有鍵/值對:

$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. 數(shù)組的差集 array_diff()

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

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

實(shí)例如下:

$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. 關(guān)聯(lián)數(shù)組的差集 array_diff_assoc()

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

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

下面的例子只返回了[yellow] => Banana,因?yàn)檫@個(gè)特殊的鍵/值對出現(xiàn)在$fruit1中,而在$fruit2和$fruit3中都不存在。

$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 )

?>

使用數(shù)組的過程中經(jīng)常要遍歷數(shù)組。通常需要遍歷數(shù)組并獲得各個(gè)鍵或值(或者同時(shí)獲得鍵和值),所以毫不奇怪,PHP為此提供了一些函數(shù)來滿足需求。許多函數(shù)能完成兩項(xiàng)任務(wù),不僅能獲取當(dāng)前指針位置的鍵或值,還能將指針移向下一個(gè)適當(dāng)?shù)奈恢谩?/p>



10. 獲取當(dāng)前數(shù)組鍵 key()

key()函數(shù)返回input_array中當(dāng)前指針?biāo)谖恢玫逆I。其形式如下:

mixed key(array array)

下面的例子通過迭代處理數(shù)組并移動(dòng)指針來輸出$fruits數(shù)組的鍵:

$fruits = array("apple"=>"red", "banana"=>"yellow");

while ($key = key($fruits)) {

printf("%s
", $key);

next($fruits);

}

// apple

// banana

注意,每次調(diào)用key()時(shí)不會(huì)移動(dòng)指針。為此需要使用next()函數(shù),這個(gè)函數(shù)的唯一作用就是完成推進(jìn)指針的任務(wù)。



11. 獲取當(dāng)前數(shù)組值 current()

current()函數(shù)返回?cái)?shù)組中當(dāng)前指針?biāo)谖恢玫臄?shù)組值。其形式如下:

mixed current(array array)

下面修改前面的例子,這一次我們要獲取數(shù)組值:

$fruits = array("apple"=>"red", "banana"=>"yellow");

while ($fruit = current($fruits)) {

printf("%s
", $fruit);

next($fruits);

}

// red

// yellow

12. 獲取當(dāng)前數(shù)組鍵和值 each()

each()函數(shù)返回input_array的當(dāng)前鍵/值對,并將指針推進(jìn)一個(gè)位置。其形式如下:

array each(array array)

返回的數(shù)組包含四個(gè)鍵,鍵0和key包含鍵名,而鍵1和value包含相應(yīng)的數(shù)據(jù)。如果執(zhí)行each()前指針位于數(shù)組末尾,則返回false。

$fruits = array("apple", "banana", "orange", "pear");

print_r ( each($fruits) );

// Array ( [1] => apple [value] => apple [0] => 0 [key] => 0 )

each() 經(jīng)常和 list() 結(jié)合使用來遍歷數(shù)組。本例與上例類似,不過循環(huán)輸出了整個(gè)數(shù)組:

$fruits = array("apple", "banana", "orange", "pear");

reset($fruits);

while (list($key, $val) = each($fruits))

{

echo "$key => $val
";

}

// 0 => apple

// 1 => banana

// 2 => orange

// 3 => pear

因?yàn)閷⒁粋€(gè)數(shù)組賦值給另一個(gè)數(shù)組時(shí)會(huì)重置原來的數(shù)組指針,因此在上例中如果我們在循環(huán)內(nèi)部將 $fruits 賦給了另一個(gè)變量的話將會(huì)導(dǎo)致無限循環(huán)。

這就完成了數(shù)組的遍歷。

查找、篩選與搜索數(shù)組元素是數(shù)組操作的一些常見功能。下面來介紹一下幾個(gè)相關(guān)的函數(shù)。



13. in_array()函數(shù)

in_array()函數(shù)在一個(gè)數(shù)組匯總搜索一個(gè)特定值,如果找到這個(gè)值返回true,否則返回false。其形式如下:

boolean in_array(mixed needle,array haystack[,boolean strict]);

來看下面的例子,查找變量apple是否已經(jīng)在數(shù)組中,如果在,則輸出一段信息:

$fruit = "apple";

$fruits = array("apple","banana","orange","pear");

if( in_array($fruit,$fruits) )

echo "$fruit 已經(jīng)在數(shù)組中";

第三個(gè)參數(shù)可選,它強(qiáng)制in_array()在搜索時(shí)考慮類型。



14. array_key_exists()函數(shù)

如果在一個(gè)數(shù)組中找到一個(gè)指定的鍵,函數(shù)array_key_exists()返回true,否則返回false。其形式如下:

boolean array_key_exists(mixed key,array array); 下面的例子將在數(shù)組鍵中搜索apple,如果找到,將輸出這個(gè)水果的顏色:

$fruit["apple"] = "red";

$fruit["banana"] = "yellow";

$fruit["pear"] = "green";

if(array_key_exists("apple", $fruit)){

printf("apple's color is %s",$fruit["apple"]);

}

//apple's color is red



15. array_search()函數(shù)

array_search()函數(shù)在一個(gè)數(shù)組中搜索一個(gè)指定的值,如果找到則返回相應(yīng)的鍵,否則返回false。其形式如下:

mixed array_search(mixed needle,array haystack[,boolean strict])

下面的例子在$fruits中搜索一個(gè)特定的日期(December 7),如果找到,則返回相應(yīng)州的有關(guān)信息:

$fruits["apple"] = "red";

$fruits["banana"] = "yellow";

$fruits["watermelon"]="green";

$founded = array_search("green", $fruits);

if($founded)

printf("%s was founded on %s.",$founded, $fruits[$founded]);

//watermelon was founded on green.



16. array_keys()函數(shù)

array_keys()函數(shù)返回一個(gè)數(shù)組,其中包含所搜索數(shù)組中找到的所有鍵。其形式如下:

array array_keys(array array[,mixed search_value])

如果包含可選參數(shù)search_value,則只會(huì)返回與該值匹配的鍵。下面的例子將輸出$fruit數(shù)組中找到的所有數(shù)組:

$fruits["apple"] = "red";

$fruits["banana"] = "yellow";

$fruits["watermelon"]="green";

$keys = array_keys($fruits);

print_r($keys);

//Array ( [0] => apple [1] => banana [2] => watermelon )



17. array_values()函數(shù)

array_values()函數(shù)返回一個(gè)數(shù)組中的所有值,并自動(dòng)為返回的數(shù)組提供數(shù)值索引。其形式如下:

array array_values(array array)

下面的例子將獲取$fruits中找到的各元素的值:

$fruits["apple"] = "red";

$fruits["banana"] = "yellow";

$fruits["watermelon"]="green";

$values = array_values($fruits);

print_r($values);

//Array ( [0] => red [1] => yellow [2] => green )

有時(shí)候我們需要擴(kuò)展一個(gè)數(shù)組,或者刪掉數(shù)組的一部分,PHP為擴(kuò)展和縮小數(shù)組提供了一些函數(shù)。對于那些希望模仿各種隊(duì)列實(shí)現(xiàn)(FIFO、LIFO)的程序員來說,這些函數(shù)可以提供便利。顧名思義,從這些函數(shù)的函數(shù)名(push、pop、shift和unshift)就清楚地反映出其作用。PS:傳統(tǒng)的隊(duì)列是一種數(shù)據(jù)結(jié)構(gòu),刪除元素與加入元素的順序相同,就稱為先進(jìn)先出,或FIFO。相反,棧是另外一種數(shù)據(jù)結(jié)構(gòu),其中刪除元素的順序與加入時(shí)的順序相反,這成為后進(jìn)先出,或LIFO。



18. 在數(shù)組頭添加元素

array_unshift()函數(shù)在數(shù)組頭添加元素。所有己有的數(shù)值鍵都會(huì)相應(yīng)地修改,以反映其在數(shù)組中的新位置,但是關(guān)聯(lián)鍵不受影響。其形式如下:

int array_unshift(array array,mixed variable[,mixed variable])

下面這個(gè)例子在$fruits數(shù)組前面添加了兩種水果:

$fruits = array("apple","banana");

array_unshift($fruits,"orange","pear")

// $fruits = array("orange","pear","apple","banana");



19. 在數(shù)組尾添加元素

array_push()函數(shù)的返回值是int型,是壓入數(shù)據(jù)后數(shù)組中元素的個(gè)數(shù),可以為此函數(shù)傳遞多個(gè)變量作為參數(shù),同時(shí)向數(shù)組壓入多個(gè)變量。其形式為:

(array array,mixed variable [,mixed variable...])

下面這個(gè)例子在$fruits數(shù)組中又添加了兩個(gè)水果:

$fruits = array("apple","banana");

array_push($fruits,"orange","pear")

//$fruits = array("apple","banana","orange","pear")



20. 從數(shù)組頭刪除值

array_shift()函數(shù)刪除并返回?cái)?shù)組中找到的元素。其結(jié)果是,如果使用的是數(shù)值健,則所有相應(yīng)的值都會(huì)下移,而使用關(guān)聯(lián)鍵的數(shù)組不受影響。其形式為

mixed array_shift(array array) 下面的例子刪除了$fruits數(shù)組中的第一個(gè)元素apple:

$fruits = array("apple","banana","orange","pear");

$fruit = array_shift($fruits);

// $fruits = array("banana","orange","pear")

// $fruit = "apple";

21. 從數(shù)組尾刪除元素

array_pop()函數(shù)刪除并返回?cái)?shù)組的最后一個(gè)元素。其形式為:

mixed array_pop(aray target_array);

下面的例子從$states數(shù)組刪除了最后的一個(gè)州:

$fruits = array("apple","banana","orange","pear");

$fruit = array_pop($fruits);

//$fruits = array("apple","banana","orange");

//$fruit = "pear";


猜你喜歡:

PHP輕松入門視頻教程

黑馬PHP培訓(xùn)課程


本文版權(quán)歸傳智播客PHP學(xué)院所有,歡迎轉(zhuǎn)載,轉(zhuǎn)載請注明作者出處。謝謝!
 
作者:傳智播客PHP培訓(xùn)學(xué)院
 
首發(fā):http://php.itcast.cn/ 

0 分享到:
和我們在線交談!