PHP的此內置函數用於刪除或彈出並返回作為參數傳遞給它的數組中的最後一個元素。由於從數組中刪除了最後一個元素,因此將數組的大小減小了一個。
用法:
array_pop($array)
參數:該函數僅接受一個參數$array,即輸入數組,並從中彈出最後一個元素,從而將大小減小一個。
返回值:此函數返回數組的最後一個元素。如果數組為空或輸入參數不是數組,則返回NULL。
注意:使用此函數後,將重置輸入數組的(reset())數組指針。
例子:
Input : $array = (1=>"ram", 2=>"krishna", 3=>"aakash"); Output : aakash Input : $array = (24, 48, 95, 100, 120); Output : 120
以下示例程序旨在說明PHP中的array_pop()函數:
例子1
<?php
// PHP code to illustrate the use of array_pop()
$array = array(1=>"ram", 2=>"krishna", 3=>"aakash");
print_r("Popped element is ");
echo array_pop($array);
print_r("\nAfter popping the last element, ".
"the array reduces to: \n");
print_r($array);
?>
輸出:
Popped element is aakash After popping the last element, the array reduces to: Array ( [1] => ram [2] => krishna )
例子2
<?php
$arr = array(24, 48, 95, 100, 120);
print_r("Popped element is ");
echo array_pop($arr);
print_r("\nAfter popping the last element, ".
"the array reduces to: \n");
print_r($arr);
?>
輸出:
Popped element is 120 After popping the last element, the array reduces to: Array ( [0] => 24 [1] => 48 [2] => 95 [3] => 10 )
異常:如果傳遞了非數組,則拋出E_WARNING異常,這是運行時錯誤或警告。此警告不會停止腳本的執行。
參考:
http://php.net/manual/en/function.array-pop.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_pop() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。