In this tutorial, you will learn what are the PHP array shift and unshift functions, and how to use them.
PHP array_shift function
array_shift() function is used to remove the first element of an array.
This function takes an array as an argument (whose first element you want to remove), and it returns the removed element.
<?php $names = ['Bob', 'Mark', 'John']; $removedItem = array_shift($names); echo "Removed - $removedItem \n"; print_r($names);
Removed - Bob Array (  => Mark  => John )
PHP array unshift function
array_unshift() function is used to insert new elements to the beginning of an array.
This function takes an array as the first argument and you can pass multiple values to the rest of the arguments, but at least one value is required.
This function returns the number of elements (including the new elements).
array_unshift(array, value1, value2, value3, ...);
<?php $names = ['Bob', 'Mark', 'John']; $el_count = array_unshift($names, 'Jane', 'Alen'); echo "Number of Elements - $el_count \n"; print_r($names);
Number of Elements - 5 Array (  => Jane  => Alen  => Bob  => Mark  => John )