您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在PHP中對數(shù)組進(jìn)行增刪插操作,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
在數(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ù)組進(jìn)行增刪插操作了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。