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

Introduction

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:

Demo

<?php

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

print_r($authors);/*from   w ww  .  jav a 2s  .  co m*/
?>

Result

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

Related Topic