Home  >  Article  >  Backend Development  >  php delete last record in array

php delete last record in array

王林
王林Original
2023-05-07 14:48:07276browse

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:

 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:

 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:

 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:

 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!

Statement:
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