PHP的这个内置函数用于删除或弹出并返回作为参数传递给它的数组的最后一个元素。由于从数组中删除了最后一个元素,因此数组的大小减少了一个。
语法:
array_pop($array)
参数:该函数只有一个参数$array,即输入数组,并从中弹出最后一个元素,使数组的大小减少一个。
返回值:该函数返回数组的最后一个元素。如果数组为空或输入参数不是数组,则返回NULL。
示例:
Input : $array = (1=>"ram", 2=>"krishna", 3=>"aakash");
Output : aakash
Input : $array = (24, 48, 95, 100, 120);
Output : 120
下面的程序说明了PHP中的array_pop()函数:
<?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);
?>
output:
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);
?>
output:
Popped element is 120
After popping the last element, the array reduces to:
Array
(
[0] => 24
[1] => 48
[2] => 95
[3] => 10
)