Home > Backend Development > PHP Problem > How to delete the last element of an array in php

How to delete the last element of an array in php

青灯夜游
Release: 2023-03-16 10:10:01
Original
3194 people have browsed it

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)".

How to delete the last element of an array in php

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(&#39;apple&#39;,&#39;banana&#39;,&#39;orange&#39;);
var_dump($user);
array_pop($user);
var_dump($user);
?>
Copy after login

How to delete the last element of an array in php

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);
?>
Copy after login

How to delete the last element of an array in php

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!

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