PHP array_shift() Function

Definition and Usage

The array_shift() function removes the first element from an array, and returns the value of the removed element.

Syntax

array_shift(array)

Parameter Description
array Required. Specifies an array


Tips and Notes

Note: If the keys are numeric, all elements will get new keys, starting from 0 and increases by 1. (See example 2)

Example 1

<?php
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
echo array_shift($a);
print_r ($a);
?> 
 
OUTPUT :
Dog
Array ( [b] => Cat [c] => Horse )


Example 2

With numeric keys:
<?php
$a=array(0=>"Dog",1=>"Cat",2=>"Horse");
echo array_shift($a);
print_r ($a);
?> 
 
OUTPUT :
Dog
Array ( [0] => Cat [1] => Horse )