Reputation: 137
I'd like to change the beginning of my array in PHP. Currently I've got:
Array
(
[0] => Bla##
[1] => Bla##
[2] => Bla##
[3] => Bla##
[4] => Bla##
[5] => Bla##
but I want
Array
(
[6] => Bla##
[7] => Bla##
[8] => Bla##
[9] => Bla##
[10] => Bla##
[11] => Bla##
I used array_splice($array, 14, 0, 'Bla##');
to insert a value at a specific index of my array, but if I use this my array starts from 0 and not from 6.
Thanks in advance!
Upvotes: 2
Views: 94
Reputation: 98921
You can use array_walk()
:
$n = 6;
array_walk($arr, function($value) use (&$x, &$n) {$x[$n] = $value; $n++;});
Upvotes: 0
Reputation: 1787
$shift = 6;
$array = array_combine(range($shift, count($array)+$shift-1), $array);
Upvotes: 5
Reputation: 2302
Just shift all the array positions forward by 6 0->6, 1->7 etc
$array = [Bla##,Bla##,Bla##,Bla##,Bla##,Bla##];
$newarray = array(); // Shifted array
for ($i=0; $i < count($array);$i++) {
$newarray[$i+6] = $array[$i];
}
Upvotes: 2