THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP array_shift() Function

PHP Array Reference PHP Array Reference

Example

Remove the first element (red) from an 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 example »

Definition and Usage

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

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


Syntax

array_shift(array)

Parameter Description
array Required. Specifies an array

Technical Details

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

More Examples

Example 1

Using numeric keys:

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

PHP Array Reference PHP Array Reference