PHP array_shift() Function

❮ 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);
?>
Try it Yourself »

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 Values

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

Using numeric keys:

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

❮ PHP Array Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.