Definition
The array_unshift() function inserts one or more elements at the beginning of an array. Numeric keys will start at 0 and increase by 1. String keys will remain the same.
Syntax
array_unshift(array, value1, value2, value3, ...)
Parameters
Parameter | Description |
---|---|
array |
Required. Specifying an array. |
value1 |
Optional. Specifies a value to insert. |
value2 |
Optional. Specifies a value to insert. |
value3... |
Optional. Specifies a value to insert. |
Example
<?php
// Example 1
$cities = array("New York", "Salt Lake", "Tokyo");
array_unshift($cities, "Berlin", "London");
print_r($cities);
echo "<br>";
// Example 2
$ages = array("Mark" => 22, "Jeff" => 32, "Mike" => 28);
array_unshift($ages, 45);
print_r($ages);
echo "<br>";