Latest web development tutorials

PHP array_unshift () function

PHP Array Reference Complete PHP Array Reference

Examples

Insert Element "blue" to the array:

<?php
$a=array("a"=>"red","b"=>"green");
array_unshift($a,"blue");
print_r($a);
?>

Running instance »

Definition and Usage

array_unshift () function is used to insert new elements in the array. Value of the new array will be inserted at the beginning of the array.

Tip: You can insert one or more values.

Note: The key name value from 0 to 1 increments.String keys will remain unchanged.


grammar

array_unshift( array,value1,value2,value3... )

参数 描述
array 必需。规定数组。
value1 必需。规定插入的值。
value2 可选。规定插入的值。
value3 可选。规定插入的值。

technical details

return value: Returns the number of new elements in the array.
PHP version: 4+


More examples

Example 1

Show the return value:

<?php
$a=array("a"=>"red","b"=>"green");
print_r(array_unshift($a,"blue"));
?>

Running instance »

Example 2

Using numeric keys:

<?php
$a=array(0=>"red",1=>"green");
array_unshift($a,"blue");
print_r($a);
?>

Running instance »


PHP Array Reference Complete PHP Array Reference