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

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

黄舟
Release: 2023-03-17 08:10:01
Original
2764 people have browsed it

实例

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

<?php
$a=array("red","green","blue");
array_pop($a);
print_r($a);
?>
Copy after login

定义和用法

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

语法

array_pop(array)
Copy after login
参数描述
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";    
 
?>
Copy after login

显示结果如下: 

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template