Home > Article > Backend Development > How to remove the last array element in php
Two removal methods: 1. Use the array_pop() function to delete elements at the end of the array, with the syntax "array_pop ($arr)"; 2. Use the array_splice() function to delete part of the array elements. Just set the second parameter to "-1", the syntax is "array_splice($arr,-1)".
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
Method 1: Use array_pop() Function
array_pop() function is used to delete elements at the end of the array.
<?php header("Content-type:text/html;charset=utf-8"); $arr=array(10,12,20,25,24); echo "原数组:"; var_dump($arr); echo "删除后的数组:" ; array_pop($arr); var_dump($arr); ?>
Output result:
#Method 2: Use array_splice() function
array_splice() function To delete part of the array elements; set the second parameter of the array_splice() function to -1 to delete the last array element.
Example:
<?php header("Content-type:text/html;charset=utf-8"); $arr=array(10,12,20,25,24); echo "删除后的数组:" ; array_splice($arr,-1); var_dump($arr); ?>
Output result:
Recommended learning:php training
The above is the detailed content of How to remove the last array element in php. For more information, please follow other related articles on the PHP Chinese website!