如何插入一个项目在PHP数组的开始? [英] How to insert a item to the beginning of an array in PHP?

查看:88
本文介绍了如何插入一个项目在PHP数组的开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道了如何通过将其插入到最后:

I know how to insert it to the end by:

$arr[] = $item;

但如何将其插入到开始?

But how to insert it to the beginning?

推荐答案

使用 array_unshift($数组$项目);

$arr = array('item2', 'item3', 'item4');
array_unshift($arr , 'item1');
print_r($arr);

会给你

Array
(
 [0] => item1
 [1] => item2
 [2] => item3
 [3] => item4
)

这篇关于如何插入一个项目在PHP数组的开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆