Home > php教程 > php手册 > body text

php数组函数序列之array_unshift() 在数组开头插入一个或多个元素

WBOY
Release: 2016-06-13 12:04:08
Original
1664 people have browsed it

array_unshift()定义和用法
array_unshift() 函数在数组开头插入一个或多个元素。

被加上的元素作为一个整体添加,这些元素在数组中的顺序和在参数中的顺序一样。

该函数会返回数组中元素的个数。

语法
array_unshift(array,value1,value2,value3...)参数 描述
array 必需。规定输入的数组。
value1 必需。规定插入的值。
value2 可选。规定插入的值。
value3 可选。规定插入的值。

提示和注释
注释:所有的数值键名将修改为从零开始重新计数,所有的字符串键名保持不变。

例子 1

复制代码 代码如下:


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


输出:

Array ( [0] => Horse [a] => Cat [b] => Dog )例子 2
返回键值:

复制代码 代码如下:


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


输出:

3例子 3
数组带有数值键:

复制代码 代码如下:


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


输出:

Array ( [0] => Horse [1] => Cat [2] => Dog )
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!