Home >Backend Development >PHP Problem >How to remove the last value of an array in php
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
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
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
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
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!