百科狗-知识改变命运!

array_unshift() - 在数组开头插入一个或多个单元 - php 数组函数

是丫丫呀2年前 (2023-11-21)阅读数 21#技术干货
文章标签数组

array_unshift()

(PHP 4, PHP 5, PHP 7)

在数组开头插入一个或多个单元

说明

array_unshift(array &$array[,mixed $...]): int

array_unshift()将传入的单元插入到$array数组的开头。注意单元是作为整体被插入的,因此传入单元将保持同样的顺序。所有的数值键名将修改为从零开始重新计数,所有的文字键名保持不变。

参数

$array

输入的数组。

array_unshift() - 在数组开头插入一个或多个单元 - php 数组函数

插入的变量。

返回值

返回$array数组新的单元数目。

更新日志

版本说明
7.3.0现在可以只用一个参数来调用,之前至少需要两个参数。

范例

Example #1array_unshift()例子


以上例程会输出:

Array
(
    [0] => apple
    [1] => raspberry
    [2] => orange
    [3] => banana
)

参见

  • array_shift()将数组开头的单元移出数组
  • array_push()将一个或多个单元压入数组的末尾(入栈)
  • array_pop()弹出数组最后一个单元(出栈)
You can preserve keys and unshift an array with numerical indexes in a really simple way if you'll do the following:

now the array looks as follows:
array(
100=>'Test Element 1 ',
255=>'Test Element 2'
224=>'someword1',
228=>'someword2',
102=>'someword3',
544=>'someword3',
95=>'someword4'
);
array_merge() will also reindex (see array_merge() manual entry), but the '+' operator won't, so...

does the job.
Sahn's example almost works but has a small error. Try it like this if you need to prepend something to the array without the keys being reindexed and/or need to prepend a key value pair, you can use this short function: 

Anonymous' associative version wasn't working for me, but it did with this small tweak:
function array_unshift_assoc(&$arr, $key, $val) 
{ 
  $arr = array_reverse($arr, true); 
  $arr[$key] = $val; 
  $arr = array_reverse($arr, true); 
  return $arr;
}
even simpler unshifting of a reference !

Actually this problem with the keys getting reindexed only happens when the keys are numerical:

Array
(
  [f] => five
  [s] => six
  [t] => twenty
)
k: f v: five
k: s v: six
k: t v: twenty
Array
(
  [0] => zero
  [f] => five
  [s] => six
  [t] => twenty
)
k: 0 v: zero
k: f v: five
k: s v: six
k: t v: twenty
This becomes a nice little problem if you index your arrays out of order (while manually sorting). For example:

Produces:
singleMonth: 3/%/2007
singleMonth: 2/%/2007
singleMonth: 1/%/2007
singleMonth: 8/%/2006
singleMonth: 7/%/2004
singleMonth: 12/%/2000
singleMonth: 11/%/2000
singleMonth: 10/%/2000
singleMonth: %
singleMonth: 8/%/2006
singleMonth: 7/%/2004
singleMonth: 3/%/2007
singleMonth: 2/%/2007
singleMonth: 12/%/2000
singleMonth: 11/%/2000
singleMonth: 10/%/2000
singleMonth: 1/%/2007 
It reindexes them based on the order they were created. It seems like if an array has all numeric indexes, then it should reindex them based on the order of their index. Just my opinion...
Another way to tack something to the beginning of an array is with array_merge().
$plans = array('AARP'=>'Senior', 'AAA'=>'Automobile Club');
$plans = array_merge(array("BAR"=>"Best Available Rate"), $plans);
If you need to prepend something to the array without the keys being reindexed and/or need to prepend a key value pair, you can use this short function:

I had a need tonight to convert a numeric array from 1-based to 0-based, and found that the following worked just fine due to the "side effect" of renumbering:

If you need to change the name of a key without changing its position in the array this function may be useful.

Last version of PHP deprecated unshifting of a reference.
You can use this function instead :

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)
{if $zbp->Config('yd1125')->foot}