PHP
Using PHP To Remove Last Array Element / Item
Once again, we come back to Arrays. In the previous article, we have seen how important it could be remove first array item. Now it would be really helpful to find out how to remove last array item using PHP. In this article, I am going to share very easy way to use PHP to remove last array element by making use of easy to follow examples. So read on to find out more.
How to use PHP to Remove Last Array Element / Item
Let’s assume that we an array in PHP, that contains the fruit names. So in this example, we will see how to remove the last item from this fruits array.
Example: Remove last element from an array using PHP
<?php //Declare an empty array $my_fruits_array = array('apple', 'mango', 'grapes', 'pineapple'); //Print the contents of the array. Use the <pre> tags to output the array in readable format. echo 'Following are the original array contents:<br />'; echo '<pre>'; print_r($my_fruits_array); echo '</pre>'; //Remove Last Element From Array Using PHP array_pop($my_fruits_array); //Print the contents of the array. Use the <pre> tags to output the array in readable format. echo 'Following are the array contents AFTER removing the last item from array:<br />'; echo '<pre>'; print_r($my_fruits_array); echo '</pre>'; ?>
Simple, isn’t it?
Do you know of any other ways to use PHP to remove last array element? Feel free to suggest by commenting below.