Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

array_shift()

Example

Remove the first element (e.g., “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);
?>

Definition and Usage

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

Note: If the array has numeric keys, all remaining elements will be reindexed, starting from 0 and increasing by 1 (see example below).

Syntax

array_shift(array)

Parameter Values

 

Parameter

Description

array

Required. Specifies the array to modify.

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

With numeric keys:

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