Q:

How to remove the first element from an array in PHP

0

How to remove the first element from an array in PHP

All Answers

need an explanation for this answer? contact us directly to get an explanation for this 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.

Let's try out the following example to understand how this method works:

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP  and MySQL Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to add elements to the beginning of an array i... >>
<< How to remove the last element from an array in PH...