PHP

Using PHP To Remove First Array Element / Item

Arrays are an important concept in PHP. So while it’s easy to create & destroy a complete array, it can be immensely useful to find out how to remove first array item using PHP. In this article, I am going to share very easy way to use PHP to remove first array element by making use of easy to follow examples. So read on to find out more.

How to use PHP to Remove First 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 first item from this fruits array.

Example: Remove first 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 First Element From Array Using PHP
array_shift($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 first 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 first array element? Feel free to suggest by commenting below.

Share your thoughts, comment below now!

*

*