Home >>PHP Array Functions >PHP array_shift() Function
PHP array_shift() function is used to remove the first element from an given input array and returns the value of the removed element. After removing the first element from the given array the keys of the remaining elements are modified and re-indexed from the start if the keys are numerical. It accepts only a single argument $array which is the original input array which needs to be shifted.
Syntax:
Syntax: array_shift($array);
Parameter | Description |
---|---|
array | This is a required parameter. This parameter defines the given array. |
Here is an example of array_shift() function in PHP:
<html> <body> <pre> <?php $a=array("Audi","BMW","Jaguar","Lamborghini","Ferrari"); echo array_shift($a)."<br>"; print_r ($a); ?> </pre> </body> </html>
Audi Array ( [0] => BMW [1] => Jaguar [2] => Lamborghini [3] => Ferrari )
Example 2:
<html> <body> <pre> <?php $a=array("A","B","H","I","M","A","N","Y","U"); echo "Original Array: <br>"; print_r($a); echo "<br>"; echo "Removed element: ".array_shift($a)."<br><br>"; echo "Shifted Array: <br>"; print_r ($a); ?> </pre> </body> </html>
Original Array: Array ( [0] => A [1] => B [2] => H [3] => I [4] => M [5] => A [6] => N [7] => Y [8] => U ) Removed element: A Shifted Array: Array ( [0] => B [1] => H [2] => I [3] => M [4] => A [5] => N [6] => Y [7] => U )