In PHP, there are two different ways to delete the last record of an array:
Method 1: Use the array_pop() function
The array_pop() function is used to remove the last record from the array Remove and return the last element. Therefore, using this function can delete the last record of the array. The following is a sample code using the array_pop() function:
<?php $myArray = array('apple', 'banana', 'orange', 'grape'); $lastElement = array_pop($myArray); print_r($myArray); // 输出:Array ( [0] => apple [1] => banana [2] => orange ) ?>
In the above sample code, an array $myArray containing 4 elements is defined. Then, use array_pop() function to remove the last element, i.e. 'grape', from the array. Finally, use the print_r() function to output the remaining elements.
If you want to delete multiple elements of the array continuously, you can use the array_pop() function multiple times. For example:
<?php $myArray = array('apple', 'banana', 'orange', 'grape'); array_pop($myArray); array_pop($myArray); print_r($myArray); // 输出:Array ( [0] => apple ) ?>
In the above sample code, the last element 'grape' is first deleted using the array_pop() function, and then the penultimate element 'orange' is deleted using the function again.
Method 2: Use the array_splice() function
The array_splice() function is used to remove elements from an array and replace them with new elements. If you do not need to replace it with a new element, you can set the third parameter to 0. Below is the sample code that uses the array_splice() function to delete the last element of the array:
<?php $myArray = array('apple', 'banana', 'orange', 'grape'); array_splice($myArray, -1); print_r($myArray); // 输出:Array ( [0] => apple [1] => banana [2] => orange ) ?>
In the above sample code, the last element of the array, which is 'grape', is deleted using the array_splice() function. The first parameter specifies the array to be modified, the second parameter specifies the starting position to delete, and the third parameter specifies the number of elements to delete (default is 1).
If you want to delete multiple elements of the array continuously, you can set the third parameter to the total number to be deleted. For example:
<?php $myArray = array('apple', 'banana', 'orange', 'grape'); array_splice($myArray, -2, 2); print_r($myArray); // 输出:Array ( [0] => apple ) ?>
In the above sample code, the second to last and last elements, namely 'orange' and 'grape', are removed using the array_splice() function. The second parameter specifies the starting position to be deleted, which is the length of the array minus the number of elements to be deleted, which is -2. The third parameter specifies the number of elements to be deleted, which is 2.
Summary
Using both the array_pop() function and the array_splice() function can delete the last record of the array. The array_pop() function is used to delete and return the last element, and the array_splice() function is used to delete and replace elements. Depending on the specific situation and performance requirements, the appropriate method can be selected.
The above is the detailed content of php delete last record in array. For more information, please follow other related articles on the PHP Chinese website!