Home  >  Article  >  Backend Development  >  How to remove the last array element in php

How to remove the last array element in php

青灯夜游
青灯夜游Original
2021-09-17 19:09:283187browse

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

How to remove the last array element in php

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:

How to remove the last array element in php

#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:

How to remove the last array element in php

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!

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