In PHP, there are many ways to delete the last element of an array. Let’s explore a few common methods below.
- Use the array_pop function
The array_pop function removes and returns the last element of an array. On this basis, we can delete the last element through the following code:
$array = array(1, 2, 3, 4, 5); array_pop($array); print_r($array); // 输出 Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
Through this method, the last element can be deleted directly, but the value of the last element cannot be obtained while deleting.
- Use the unset function
Use the unset function to delete elements based on the array subscript. We can delete the last element of the array through the following code:
$array = array(1, 2, 3, 4, 5); unset($array[count($array) - 1]); print_r($array); // 输出 Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
Among them, the count function is used to get the length of the array (that is, the number of elements), and get the subscript of the last element by subtracting 1. However, this method cannot obtain the value of the last element while deleting.
- Use the array_splice function
The array_splice function can remove elements from an array and return the cut elements. We can delete the last element of the array through the following code:
$array = array(1, 2, 3, 4, 5); array_splice($array, -1); print_r($array); // 输出 Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
Among them, -1 means starting from the last element of the array. Use this method to simultaneously delete the last element and get its value.
- Use the array_slice function
The array_slice function can return a slice from an array. We can obtain all elements of the array except the last element through the following code:
$array = array(1, 2, 3, 4, 5); $array = array_slice($array, 0, -1); print_r($array); // 输出 Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
Among them, the first parameter indicates the array to be sliced, the second parameter indicates the starting position to be cut, and the third parameter The parameter indicates the length to be cut. We set the second parameter to 0, which starts cutting from the first element of the array, and the third parameter to -1, which ends cutting from the last element of the array. This method can get all elements except the last element. If you want to delete the last element directly, you can assign the result returned by this method to the original array variable.
To sum up, the above is the method to delete the last element of the array in PHP. We can choose different methods to delete or get the last element of the array according to different situations.
The above is the detailed content of php delete array last. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!