PHP | array_shift() Function
This inbuilt function of PHP removes the first element from an array and returns the value of the removed element. After the removal of the first element, the key of the remaining elements is modified and again re-numbered from the start, only if the keys are numerical. In other words, this function basically shifts an element off from the beginning in an array.
Syntax:
array_shift($array)
Parameters: The function takes only one argument, $array which refers to the original input array which needs to be shifted.
Return Value: As already mentioned, the function returns the value of the shifted element from the array, otherwise NULL if the array is empty.
Examples:
Input : $array = ("ram"=>2, "aakash"=>4, "saran"=>5, "mohan"=>100)
Output : 2
Input : $array = (45, 5, 1, 22, 22, 10, 10);
Output :45
In this program, we will see how the function works in key_value pair array.
<?php // PHP function to illustrate the use of array_shift() function Shifting($array) { print_r(array_shift($array)); echo "\n"; print_r($array); } $array = array("ram"=>2, "aakash"=>4, "saran"=>5, "mohan"=>100); Shifting($array); ?> |
Output:
2
Array
(
[aakash] => 4
[saran] => 5
[mohan] => 100
)
Now let’s see how the function takes care of the default key.
<?php // PHP function to illustrate the use of array_shift() function Shifting($array) { print_r(array_shift($array)); echo "\n"; print_r($array); } $array = array(45, 5, 1, 22, 22, 10, 10); Shifting($array); ?> |
Output:
45
Array
(
[0] => 5
[1] => 1
[2] => 22
[3] => 22
[4] => 10
[5] => 10
)
Reference:
http://php.net/manual/en/function.array-shift.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- D3.js | d3.max() function
- PHP | Ds\Set add() Function
- PHP | Ds\Set xor() Function
- PHP | Ds\Map put() Function
- PHP | Ds\Map xor() Function
- PHP | each() Function
- PHP | pow( ) Function
- PHP | key() Function
- CSS | rgb() Function
- PHP | min( ) Function
- PHP | pos() Function
- p5.js | hex() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai



