How to remove the first element from an array in PHP
Topic: PHP / MySQLPrev|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
.
Let's try out the following example to understand how this method works:
Example
Try this code »<?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: