如何在 PHP 中的数组开头插入一个项目? [英] How to insert an item at the beginning of an array in PHP?
本文介绍了如何在 PHP 中的数组开头插入一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道如何将它插入到最后:
I know how to insert it to the end by:
$arr[] = $item;
但是如何插入到开头呢?
But how to insert it to the beginning?
推荐答案
使用 array_unshift($array, $item);
$arr = array('item2', 'item3', 'item4');
array_unshift($arr , 'item1');
print_r($arr);
会给你
Array
(
[0] => item1
[1] => item2
[2] => item3
[3] => item4
)
这篇关于如何在 PHP 中的数组开头插入一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文