Monday, 13 August 2018

How to remove the first element from an array in PHP

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.

Example

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

0 comments:

Post a Comment