How to remove the first element from an array in PHP

  234 views   3 months ago 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.

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);
?>

 

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com

Related Articles