PHP中對數(shù)組的一些常用的增、刪、插操作函數(shù)總結(jié)
有時候我們需要擴展一個數(shù)組,或者刪掉數(shù)組的一部分,PHP為擴展和縮小數(shù)組提供了一些函數(shù)。對于那些希望模仿各種隊列實現(xiàn)(FIFO、LIFO)的程序員來說,這些函數(shù)可以提供便利。顧名思義,從這些函數(shù)的函數(shù)名(push、pop、shift和unshift)就清楚地反映出其作用。
PS:傳統(tǒng)的隊列是一種數(shù)據(jù)結(jié)構(gòu),刪除元素與加入元素的順序相同,就稱為先進先出,或FIFO。相反,棧是另外一種數(shù)據(jù)結(jié)構(gòu),其中刪除元素的順序與加入時的順序相反,這成為后進先出,或LIFO。
在數(shù)組頭添加元素
array_unshift()函數(shù)在數(shù)組頭添加元素。所有己有的數(shù)值鍵都會相應(yīng)地修改,以反映其在數(shù)組中的新位置,但是關(guān)聯(lián)鍵不受影響。其形式如下:
int array_unshift(array array,mixed variable[,mixed variable])
下面這個例子在$fruits數(shù)組前面添加了兩種水果:
$fruits = array("apple","banana");
array_unshift($fruits,"orange","pear")
// $fruits = array("orange","pear","apple","banana");
在數(shù)組尾添加元素
array_push()函數(shù)的返回值是int型,是壓入數(shù)據(jù)后數(shù)組中元素的個數(shù),可以為此函數(shù)傳遞多個變量作為參數(shù),同時向數(shù)組壓入多個變量。其形式為:
(array array,mixed variable [,mixed variable...])
下面這個例子在$fruits數(shù)組中又添加了兩個水果:
$fruits = array("apple","banana");
array_push($fruits,"orange","pear")
//$fruits = array("apple","banana","orange","pear")
從數(shù)組頭刪除值
array_shift()函數(shù)刪除并返回數(shù)組中找到的元素。其結(jié)果是,如果使用的是數(shù)值健,則所有相應(yīng)的值都會下移,而使用關(guān)聯(lián)鍵的數(shù)組不受影響。其形式為:
mixed array_shift(array array)
下面的例子刪除了$fruits數(shù)組中的第一個元素apple:
$fruits = array("apple","banana","orange","pear");
$fruit = array_shift($fruits);
// $fruits = array("banana","orange","pear")
// $fruit = "apple";
從數(shù)組尾刪除元素
array_pop()函數(shù)刪除并返回數(shù)組的最后一個元素。其形式為:
mixed array_pop(aray target_array);
下面的例子從$states數(shù)組刪除了最后的一個州:
$fruits = array("apple","banana","orange","pear");
$fruit = array_pop($fruits);
//$fruits = array("apple","banana","orange");
//$fruit = "pear";
查找、篩選與搜索數(shù)組元素是數(shù)組操作的一些常見功能。下面來介紹一下幾個相關(guān)的函數(shù)。
in_array()函數(shù)
in_array()函數(shù)在一個數(shù)組匯總搜索一個特定值,如果找到這個值返回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ù)組中";
第三個參數(shù)可選,它強制in_array()在搜索時考慮類型。
array_key_exists()函數(shù)
如果在一個數(shù)組中找到一個指定的鍵,函數(shù)array_key_exists()返回true,否則返回false。其形式如下:
boolean array_key_exists(mixed key,array array);
下面的例子將在數(shù)組鍵中搜索apple,如果找到,將輸出這個水果的顏色:
$fruit["apple"] = "red";
$fruit["banana"] = "yellow";
$fruit["pear"] = "green";
if(array_key_exists("apple", $fruit)){
printf("apple's color is %s",$fruit["apple"]);
}
執(zhí)行這段代碼得到的結(jié)果:
apple's color is red
array_search()函數(shù)
array_search()函數(shù)在一個數(shù)組中搜索一個指定的值,如果找到則返回相應(yīng)的鍵,否則返回false。其形式如下:
mixed array_search(mixed needle,array haystack[,boolean strict])
下面的例子在$fruits中搜索一個特定的日期(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])
程序運行結(jié)果如下:
watermelon was founded on green.
array_keys()函數(shù)
array_keys()函數(shù)返回一個數(shù)組,其中包含所搜索數(shù)組中找到的所有鍵。其形式如下:
array array_keys(array array[,mixed search_value])
如果包含可選參數(shù)search_value,則只會返回與該值匹配的鍵。下面的例子將輸出$fruit數(shù)組中找到的所有數(shù)組:
$fruits["apple"] = "red"; $fruits["banana"] = "yellow"; $fruits["watermelon"]="green"; $keys = array_keys($fruits); print_r($keys);
程序運行結(jié)果如下:
Array ( [0] => apple [1] => banana [2] => watermelon )
array_values()函數(shù)
array_values()函數(shù)返回一個數(shù)組中的所有值,并自動為返回的數(shù)組提供數(shù)值索引。其形式如下:
array array_values(array array)
下面的例子將獲取$fruits中找到的各元素的值:
$fruits["apple"] = "red"; $fruits["banana"] = "yellow"; $fruits["watermelon"]="green"; $values = array_values($fruits); print_r($values);
程序運行結(jié)果如下:
Array ( [0] => red [1] => yellow [2] => green )
- PHP數(shù)組去重比較快的實現(xiàn)方式
- thinkPHP中多維數(shù)組的遍歷方法
- PHP中的數(shù)組處理函數(shù)實例總結(jié)
- 大家都應(yīng)該掌握的PHP關(guān)聯(lián)數(shù)組使用技巧
- PHP多維數(shù)組轉(zhuǎn)一維數(shù)組的簡單實現(xiàn)方法
- PHP按指定鍵值對二維數(shù)組進行排序的方法
- php刪除數(shù)組中重復(fù)元素的方法
- PHP多維數(shù)組遍歷方法(2種實現(xiàn)方法)
- 詳解PHP對數(shù)組的定義以及數(shù)組的創(chuàng)建方法
- php實現(xiàn)遍歷多維數(shù)組的方法
- PHP數(shù)組游標實現(xiàn)對數(shù)組的各種操作詳解

