PHP的此內置函數從數組中刪除第一個元素,並返回已刪除元素的值。刪除第一個元素後,僅當鍵為數字時,其餘元素的鍵才會被修改並從頭開始重新編號。換句話說,此函數本質上將元素從數組的開頭移開。
用法:
array_shift($array)
參數:該函數僅接受一個參數$array,它指向需要移位的原始輸入數組。
返回值:如前所述,該函數從數組返回已移位元素的值,如果數組為空,則返回NULL。
例子:
Input : $array = ("ram"=>2, "aakash"=>4, "saran"=>5, "mohan"=>100) Output : 2 Input : $array = (45, 5, 1, 22, 22, 10, 10); Output :45
在此程序中,我們將看到函數在key_value對數組中的工作方式。
<?php
// PHP function to illustrate the use of array_shift()
function Shifting($array)
{
print_r(array_shift($array));
echo "\n";
print_r($array);
}
$array = array("ram"=>2, "aakash"=>4, "saran"=>5, "mohan"=>100);
Shifting($array);
?>
輸出:
2 Array ( [aakash] => 4 [saran] => 5 [mohan] => 100 )
現在,讓我們看一下該函數如何處理默認鍵。
<?php
// PHP function to illustrate the use of array_shift()
function Shifting($array)
{
print_r(array_shift($array));
echo "\n";
print_r($array);
}
$array = array(45, 5, 1, 22, 22, 10, 10);
Shifting($array);
?>
輸出:
45 Array ( [0] => 5 [1] => 1 [2] => 22 [3] => 22 [4] => 10 [5] => 10 )
參考:
http://php.net/manual/en/function.array-shift.php
相關用法
- p5.js nfc()用法及代碼示例
- p5.js nfp()用法及代碼示例
- d3.js d3.hcl()用法及代碼示例
- p5.js nfs()用法及代碼示例
- PHP cos( )用法及代碼示例
- PHP sin( )用法及代碼示例
- p5.js nf()用法及代碼示例
- PHP tan( )用法及代碼示例
- PHP pow( )用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- d3.js d3.set.has()用法及代碼示例
- PHP Ds\Set xor()用法及代碼示例
注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 PHP | array_shift() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。