开发者

How to modify array such that all keys are increased by 1

开发者 https://www.devze.com 2023-02-18 07:43 出处:网络
I have this array $arr[0] = \'zero\' $arr[1] = \'one\' $arr[2] = \'two\' $arr[3] = \'three\' I want t开发者_StackOverflowo be able to push a value in at a certain point and push the keys which foll

I have this array

$arr[0] = 'zero'
$arr[1] = 'one'
$arr[2] = 'two'
$arr[3] = 'three'

I want t开发者_StackOverflowo 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?


array_splice($array, 2, 0, 'my new value');

http://php.net/array_splice

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号