-3
array(
      [0]=>1
      [1]=>2
      [2]=>3
      [3]=>4
)

If delete second element, you have

array(
      [0]=>1
      [2]=>3
      [3]=>4
)

Now how to change indexes to

array(
      [0]=>1
      [1]=>3
      [2]=>4
)
Mohammad Nikdouz
  • 737
  • 1
  • 6
  • 12

2 Answers2

4

If you want to re-index your array starting to zero, simply do the following:

$myNewArray = array_values($myOldArry);
Kaushal Suthar
  • 410
  • 5
  • 24
  • But this requires multiple calls and is less than desirable... using array_splice would probably be the best way to go. – dudewad Apr 26 '16 at 19:12
2

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

array_splice: Removes the elements designated by offset and length from the input array, and replaces them with the elements of the replacement array, if supplied.

So, you would use:

$arr = array(1,2,3,4);
array_splice($arr, 2, 1); //Will give you an array: [1, 2, 4]
dudewad
  • 13,215
  • 6
  • 37
  • 46