How to use array_unshift() Function in PHP

array_unshift function is used to add one or more values at the beginning of an array.

Let’s see how this works:

$bands = array('The Doors', 'Dire Sraits');
array_unshift($bands, 'The Beatles', 'Simon and Garfunkel');
print_r($bands);

Here’s the output :

Array ( [0] => The Beatles [1] => Simon and Garfunkel [2] => The Doors [3] => Dire Straits )

Two new values are added at the beginning of the array.

Click Here to Leave a Comment Below

Leave a Reply: