0

I have numbered array like

[0] => value 0
[1] => value 1
[2] => value 2
[3] => value 3
[4] => value 4
//keys 5-8 aren't set (I'm only demonstrating there could be a gap)
[9] => value 5
[10] => value 6

Is there any way to shuffle the array to move values when I add new to the specific position? So if i do something simillar to $array[1] = 'new value' it will change original value of 1 to new value of 2, 2 to 3 etc. up to 4 (then there is empty place in the example)?

Like this:

[0] => value 0
[1] => new value
[2] => value 1
[3] => value 2
[4] => value 3
[5] => value 4
[9] => value 5
[10] => value 6

Thank you very much for your help

Gomi
  • 602
  • 1
  • 6
  • 20

2 Answers2

2

Use array_splice .. Keep linear, not associated array, but places in array that need to be empty (for now) assign to null

Should work.

Community
  • 1
  • 1
animaacija
  • 110
  • 7
  • 25
1

You could use the array_slice(). Check the examples below:

http://php.net/manual/en/function.array-splice.php

Clyff
  • 3,826
  • 2
  • 14
  • 32