PHP array_shift() Function

Example

Remove the first element (red) from the array and return the value of the removed element:

<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_shift($a);
print_r ($a);
?>

Run Instance

Definition and Usage

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

Note:If the key name is numeric, all elements will receive new key names, starting from 0 and increasing by 1 (see the example below).

Syntax

array_shift(array)
Parameter Description
array Required. Specifies the array.

Technical Details

Return Value: Returns the value of the element removed from the array, or NULL if the array is empty.
PHP Version: 4+

More Examples

Example 1

Use numeric keys:

<?php
$a=array(0=>"red",1=>"green",2=>"blue");
echo array_shift($a);
print_r ($a);
?>

Run Instance