亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP中對(duì)數(shù)組的一些常用的增、刪、插操作函數(shù)總結(jié)

 更新時(shí)間:2015年11月27日 16:42:33   投稿:goldensun  
這篇文章主要介紹了PHP中對(duì)數(shù)組的一些常用的增、刪、插操作函數(shù)總結(jié),數(shù)組的操作是PHP入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下

有時(shí)候我們需要擴(kuò)展一個(gè)數(shù)組,或者刪掉數(shù)組的一部分,PHP為擴(kuò)展和縮小數(shù)組提供了一些函數(shù)。對(duì)于那些希望模仿各種隊(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。

在數(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");

在數(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")

從數(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";

從數(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";

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

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í)考慮類型。

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"]);
}

執(zhí)行這段代碼得到的結(jié)果:

apple's color is red

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

程序運(yùn)行結(jié)果如下:

watermelon was founded on green.

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

程序運(yùn)行結(jié)果如下:

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

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

程序運(yùn)行結(jié)果如下:

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

相關(guān)文章

最新評(píng)論