PHP array_shift is an inbuilt Function in PHP. It removes an element from the beginning of the array and returns it. Also, if the keys of the array are numerical, it re-indexes the remaining elements again from 0. However, it does not change literal keys. In this article, we will discuss the PHP array_shift Function. Also, we will discuss a few examples of using it.
Syntax
array_shift($array)
Parameters
The array_shift Function in PHP expects only one parameter. The array from which you need to remove the first element is passed as the parameter.
Return Value
The PHP array_shift Function returns the element it shifts from the beginning. However, if the input array is empty or invalid, it returns a NULL value.

Examples
Let’s discuss a few examples of using the PHP array_shift function.
Example 1: Simple Integer Array
For instance, consider a simple array.
<?php $testArray = array(1, 2, 3, 4); //Original Array print_r($testArray); /* Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) */ $shiftedElement = array_shift($testArray); //Shifted Element var_dump($shiftedElement); //int(1) //Reindexed Array print_r($testArray); /* Array ( [0] => 2 [1] => 3 [2] => 4 ) */ ?>
In the above example you can observe the following:
- The original array contains 4 elements.
- The Function returns the first element from the original array.
- The remaining elements are numerically re-indexed again starting from 0.
Example 2: Associative Array
For example, consider an associative array.
<?php $testArray = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4); //Original Array print_r($testArray); /* Array ( [a] => 1 [b] => 2 [c] => 3 [d] => 4 ) */ $shiftedElement = array_shift($testArray); //Shifted Element var_dump($shiftedElement); //int(1) //Reindexed Array print_r($testArray); /* Array ( [b] => 2 [c] => 3 [d] => 4 ) */ ?>
You can observe the following:
- The original array contain 4 elements with string keys.
- The function returns the first element from the original array.
- The function does not change the literal keys of the original array.
Example 3: Multi-Dimensional Array
Similarly, you can also pass multi-dimensional arrays to the function.
<?php $testArray = array( array('Monica', 'Chandler'), array('Chandler'), array('Joey') ); $shiftedElement = array_shift($testArray); //Shifted Element print_r($shiftedElement); /* Array ( [0] => Monica [1] => Chandler ) */ //Reindexed Array print_r($testArray); /* Array ( [0] => Array ( [0] => Chandler ) [1] => Array ( [0] => Joey ) ) */ ?>
Likewise, the function returns the first element of the original array. Also, it re-arranges all the remaining elements starting from index 0.
Expert Tip
This function is not very fast when it comes to big arrays. This is because, it re-indexes all the elements in the remaining array which takes linear time.
Conclusion
In this article, we discussed the array_shift Function. You can read more about it on the Official PHP Documentation. Also, you can read more about PHP Array Functions on Concatly.

Vishesh is currently working as a Lead Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.