How to use array_pop() Function In PHP
The array_pop()
function is specifically designed to remove the last element from an array and return its value.
PHP
<?php $arr = [1, 2, 3, 4, 5]; // Remove last element using // array_pop() function $lastElement = array_pop ( $arr ); // Display the modified array and // the removed element echo "Modified Array: " ; print_r( $arr ); echo "Removed Element: $lastElement" ; ?> |
Output
Modified Array: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) Removed Element: 5
How to Remove Last Element from an Array in PHP ?
Removing the last element from an array is a common operation in PHP. There are several approaches to achieve this. In this article, we will explore different methods to remove the last element from an array.
Table of Content
- Using array_pop() Function
- Using array_slice() Function
- Using unset() Function