Home  >  Article  >  Backend Development  >  PHP array function sequence array_unshift() inserts one or more elements at the beginning of the array_PHP tutorial

PHP array function sequence array_unshift() inserts one or more elements at the beginning of the array_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:22:421453browse

array_unshift() definition and usage
array_unshift() function inserts one or more elements at the beginning of the array.

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

This function will return the number of elements in the array.

Syntax
array_unshift(array,value1,value2,value3...) Parameter Description
array Required. Specifies the input 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.

Tips and Notes
Note: All numeric key names will be modified to start counting from zero, and all string key names will remain unchanged.

Example 1

Copy code The code is as follows:

$a= array("a"=>"Cat","b"=>"Dog");
array_unshift($a,"Horse");
print_r($a);
?> ;

Output:

Array ( [0] => Horse [a] => Cat [b] => Dog ) Example 2
Return key value :
Copy code The code is as follows:

$a=array("a"=> ;"Cat","b"=>"Dog");
print_r(array_unshift($a,"Horse"));
?>

Output:

3 Example 3
Array with numeric keys:
Copy code The code is as follows:

< ;?php
$a=array(0=>"Cat",1=>"Dog");
array_unshift($a,"Horse");
print_r($a);
?>

Output:

Array ( [0] => Horse [1] => Cat [2] => Dog )

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324613.htmlTechArticlearray_unshift() definition and usage array_unshift() function inserts one or more elements at the beginning of the array. The added elements are added as a whole, and the order of these elements in the array is the same as...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn