Home  >  Article  >  Backend Development  >  php function array_pop() to delete the last element in the array

php function array_pop() to delete the last element in the array

黄舟
黄舟Original
2017-11-08 10:32:352582browse

实例

删除数组中的最后一个元素:

定义和用法

array_pop() 函数删除数组中的最后一个元素。

语法

array_pop(array)
参数 描述
array 必需。规定数组。

技术细节

返回值: 返回数组的最后一个值。如果数组是空的,或者不是一个数组,将返回 NULL。
PHP 版本: 4+

现在我们试着使用PHP函数array_pop()从数组的尾端删除一些元素: 

<?   
 
/* 首先我们建立一个数组 */   
 
$fruitArray = array("apple", "orange", "banana", "Peach", "pear");    
 
/* 使用array_pop()函数从数组的尾端删除一个元素 */   
 
$popped = array_pop($fruitArray);    
 
/* 现在我们把删除后的数组中所有元素的键(key)与值(value)都显示在网页上 */   
 
while (list($key,$value) = each($fruitArray)) {   
 
echo "$key : $value<br>";   
 
}    
 
echo "<br>最后,刚才被删除的元素的值会储存在 \$popped 变量里面,它的值是:$popped";    
 
?>

显示结果如下: 

0 : apple

1 : orange

2 : banana

3 : Peach   

最后,由PHP函数array_pop()删除的元素的值会储存在 $popped 变量里面,它的值是: pear 。


The above is the detailed content of php function array_pop() to delete the last element in the array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn