PHP Function Reference

PHP array_shift() Function



The PHP array_shift() function removes the first value from an array, and returns the removed value. It returns null if array is empty or is not an array. If the returned value is not null, all numerical array keys is modified to start counting from zero while the string keys remains unaffected.

Syntax

array_shift(array)

Parameters

array Required. Specify the input array.

Return Value

Returns the removed value, or null if array is empty or is not an array.

Exceptions

NA.

Example:

The example below shows the usage of array_shift() function.

<?php
$Arr = array(10, 20, 30);

//displaying the array
print_r($Arr);
echo "\n";

//removing the first value
array_shift($Arr);

//displaying the array
print_r($Arr);
?>

The output of the above code will be:

Array
(
    [0] => 10
    [1] => 20
    [2] => 30
)

Array
(
    [0] => 20
    [1] => 30
)

Example:

Consider one more example where array_shift() function is used to remove first key-value pair from an associative array.

<?php
$Arr = array("Marry"=>25, 
             "John"=>30, 
             "Jo"=>45, 
             "Kim"=>22);

//displaying the array
print_r($Arr);
echo "\n";

//removing the first value
array_shift($Arr);

//displaying the array
print_r($Arr);
?>

The output of the above code will be:

Array
(
    [Marry] => 25
    [John] => 30
    [Jo] => 45
    [Kim] => 22
)

Array
(
    [John] => 30
    [Jo] => 45
    [Kim] => 22
)

❮ PHP Array Reference