How to remove the last value of an array in php

PHPz
Release: 2023-05-05 20:13:07
Original
2497 people have browsed it

In PHP, we can use different ways to get the last value of the array. In this article we will explore different approaches and give example code.

1. Use the end() function

PHP provides a built-in function end(), which can return the last element of the array. The following is an example code that uses the end() function to retrieve the last value of an array:

$array = array(1, 2, 3, 4, 5); $last_element = end($array); echo $last_element; // 输出 5
Copy after login

In the above code, we first define an array containing 5 elements, and then use the end() function to retrieve the last element. .

It should be noted that after using the end() function, the internal pointer of the array will point to the last element, so you need to use the reset() function to reset the pointer in order to use the array again. The following is a sample code:

$array = array(1, 2, 3, 4, 5); $last_element = end($array); echo $last_element; // 输出 5 reset($array); $first_element = current($array); echo $first_element; // 输出 1
Copy after login

In the above code, after using end() to take out the last element, we use the reset() function to reset the pointer, and then use the current() function to take out the first element of the array. an element.

2. Use the array_slice() function

In addition to using the end() function, we can also use the array_slice() function to get the last element of the array. The following is the sample code:

$array = array(1, 2, 3, 4, 5); $last_element = array_slice($array, -1, 1); echo $last_element[0]; // 输出 5
Copy after login

In the above code, we use the array_slice() function to take out an element starting from the last element, and then use index 0 to get the value of this element.

It should be noted that the array_slice() function will return a subarray, so we need to use index 0 to remove the elements in the subarray.

3. Use the key() and end() functions

If we just want to get the value of the last element of the array, we can also use the key() and end() functions in combination accomplish. The following is a sample code:

$array = array(1, 2, 3, 4, 5); $last_key = key( array_slice( $array, -1, 1, true ) ); echo $array[$last_key]; // 输出 5
Copy after login

In the above code, we use the array_slice() function to remove a subarray containing the last element of the array, and then use the key() function to obtain the key name of the subarray, and finally use This key name is used to obtain the value of the last element of the array.

It should be noted that when using the array_slice() function, you need to pass the fourth parameter true to retain the key names of the original array. Otherwise, the key() function will return a numeric index and will not correctly obtain the value of the last element of the array.

Summary

There are many ways to retrieve the last value of a PHP array. We can use the built-in function end(), array_slice() function or a combination of key() and end() functions. When using the end() function, you need to pay attention to the position of the array pointer. When using the array_slice() function, you need to pass the fourth parameter to retain the key name. When using the key() and end() functions, you need to use them in combination. In actual development, we can choose a suitable method to remove the last element of the array according to the specific situation to make the code more concise and efficient.

The above is the detailed content of How to remove the last value of an array in php. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!