How to remove last element from an array in PHP?
To remove the last element or value from an array, array_pop() function is used. This function returns the last removed element of the array and returns NULL if the array is empty, or is not an array. After removing the last element from the array, the array is modified, and the index is reduced by one from last, only if the keys are numerical.
It is an inbuilt array function of PHP, which deletes the last element of an array.
The array_pop() function, which is used to remove the last element from an array, returns the removed element. It also returns NULL, if the array is empty, or is not an array.
For example: Using string elements
An element "Skoda" is removed from the end of the given array, and the updated list is displayed in the given output.
Arraylist: Array (  => Mercedes  => Creta  => Audi  => Chevrolet  => Skoda ) Removed element from array is: Swift Updated arraylist: Array (  => Mercedes  => Creta  => Audi  => Chevrolet )
Example: Using numeric keys
Removed element: Jupiter Array (  => DLF  => GIP )
Example: Using numeric values
An element 39 is removed from the end of the given array, as you can see in the below output.
Removed array element is: 39 Update array is: Array (  => 45  => 76  => 23  => 91  => 82 )
Next TopicPHP Tutorial