PHP - Adding and Removing Elements at the Start and End via array_unshift


You can use array_unshift() to insert an element or elements at the start of an array.

The function returns the new number of elements in the array.

For example:



$authors = array("A","B","C","D");
echo array_unshift($authors,"Hardy","M")."\n"; // Displays"6"

print_r($authors);/*from  w  ww  . j  a va  2s .  com*/


You can't add key/value pairs to associative arrays using array_unshift() or its counterpart, array_pop().

Related Topic