Current Location: Home> Function Categories> array_unshift

array_unshift

Insert one or more units at the beginning of an array
Name:array_unshift
Category:Array
Programming Language:php
One-line Description:Insert one or more elements at the beginning of the array.

Definition and usage

array_unshift() function is used to insert new elements into an array. The value of the new array will be inserted at the beginning of the array.

The added elements are added as a whole, the order in the array is the same as in the parameters.

This function returns the number of elements in the array.

Tip: You can insert one or more values.

Note: The numeric key names will start at 0 and increment by 1. The string key name will remain unchanged.

Example

Example 1

Insert the element "blue" into the array:

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

Try it yourself

Example 2

Show return value:

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

Try it yourself

Example 3

Use the numeric key name:

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

Try it yourself

grammar

 array_unshift ( array , value1 , value2 , value3 ... )
parameter describe
array Required. Specify array.
value1 Required. Specifies the value to be inserted.
value2 Optional. Specifies the value to be inserted.
value3 Optional. Specifies the value to be inserted.
Similar Functions
Popular Articles