Deletion method: 1. Use array_pop() to delete the last element, the syntax is "array_pop($arr);"; 2. Use array_splice() to delete the last N elements, just set the second parameter Just "-N", the syntax is "array_splice($arr,-N)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php deletes the array at the end Several methods of elements
Method 1: Use array_pop() function to delete the last element
array_pop($array)
Function deletes the last element in the array.
This function will change the original array; the return value is the last value of the array. If the array is empty or not an array, NULL will be returned.
<?php $user=array('apple','banana','orange'); var_dump($user); array_pop($user); var_dump($user); ?>
Method 2: Use the array_splice() function to delete the last few array elements
array_splice() function is used to delete the array part of the elements; set the second parameter of the array_splice() function to -N
to delete the last N
array elements.
<?php header("Content-type:text/html;charset=utf-8"); $arr=array(10,12,20,25,24); var_dump($arr); echo "删除后的数组:" ; array_splice($arr,-1); var_dump($arr); $arr=array(10,12,20,25,24); array_splice($arr,-2); var_dump($arr); $arr=array(10,12,20,25,24); array_splice($arr,-3); var_dump($arr); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to delete the last element of an array in php. For more information, please follow other related articles on the PHP Chinese website!