Computer >> Computer tutorials >  >> Programming >> PHP

array_shift() function in PHP


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

Syntax

array_shift(arr)

Parameters

  • arr − The specified array

Return

The array_shift() function returns the shifted value. It returns NULL if the array is empty

Example

The following is an example −

<?php
$products = array("Electronics", "Accessories", "Shoes", "Toys", "Groceries");
$res = array_shift($products);
print_r($products);
?>

Output.

The first value “Electronics” gets assigned to “$res” −

Array (
   [0] => Accessories
   [1] => Shoe
   [2] => Toys
   [3] => Groceries
)