remove an element from the beginning of an array in php

To remove an element from the beginning of an array in PHP, you can use the array_shift() function. This function will remove the first element of the array and return it.

Here's an example:

main.php
$myArray = array("apple", "banana", "orange");
$removedElement = array_shift($myArray);
88 chars
3 lines

After this code is executed, $myArray will contain ["banana", "orange"], and $removedElement will contain "apple".

Note that this function will modify the original array. If you want to preserve the original array and get a copy with the first element removed, you can use the array_slice() function:

main.php
$myArray = array("apple", "banana", "orange");
$myArrayWithoutFirstElement = array_slice($myArray, 1);
103 chars
3 lines

After this code is executed, $myArray will contain ["apple", "banana", "orange"], and $myArrayWithoutFirstElement will contain ["banana", "orange"].

gistlibby LogSnag