How to use unset() Function In PHP
The unset() function can also be used to remove the last element, but it works by unsetting the array element at a specific index.
PHP
<?php $arr = [1, 2, 3, 4, 5]; // Remove the last element unset( $arr [ count ( $arr ) - 1]); // Display the modified array echo "Modified Array: " ; print_r( $arr ); ?> |
Output
Modified Array: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
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