How to remove the first element from an array in PHP

Topic: PHP / MySQL« Prev|Next »

Answer: Use the PHP array_shift() function

You can use the PHP array_shift() function to remove the first element or value from an array. The array_shift() function also returns the removed value of array. However, if the array is empty (or the variable is not an array), the returned value will be NULL.

  • <?php
  • $hobbies = array("Acting", "Drawing", "Music", "Films", "Photography");
  •  
  • // Deleting first array item
  • $removed = array_shift($hobbies);
  • print_r($hobbies);
  • echo "<br>";
  • var_dump($removed);
  • ?>

Related FAQ

Here are some more FAQ related to this topic:

 
Close

Your Feedback:

 

We would love to hear from you! Please say something.