php delete array last
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

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.

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 symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

Prepared statements in PHP enhance database security and efficiency by preventing SQL injection and improving query performance through compilation and reuse.Character count: 159

Article discusses retrieving data from databases using PHP, covering steps, security measures, optimization techniques, and common errors with solutions.Character count: 159

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 implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.
