如何修改数组,使所有键都增加 1
我有一个数组,
$arr[0] = 'zero'
$arr[1] = 'one'
$arr[2] = 'two'
$arr[3] = 'three'
我希望能够在某个点推入一个值,并将这个新值后面的键推入一个,最终得到:
$arr[0] = 'zero'
$arr[1] = 'one'
$arr[2] = 'my new value' //keys before remain the same, keys after are increased by 1
$arr[3] = 'two'
$arr[4] = 'three'
是否有一个不涉及 foreach< 的简单解决方案/code> 或者这是我唯一的解决方案?
I have this array
$arr[0] = 'zero'
$arr[1] = 'one'
$arr[2] = 'two'
$arr[3] = 'three'
I want to be able to push a value in at a certain point and push the keys which follow this new value up by one so ending up with:
$arr[0] = 'zero'
$arr[1] = 'one'
$arr[2] = 'my new value' //keys before remain the same, keys after are increased by 1
$arr[3] = 'two'
$arr[4] = 'three'
Is there a simple solution which does not involve a foreach
or is that my only solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://php.net/array_splice
http://php.net/array_splice