5

How to remove the first element from an array in PHP

 1 year ago
source link: https://www.laravelcode.com/post/how-to-remove-the-first-element-from-an-array-in-php
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to remove the first element from an array in PHP

  1164 views

  2 years 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 [email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK