1

Is there any way to have an array with items like:

$array[1];
$array[3];
$array[4];

Be turned into an array like this:

$array[1];
$array[2];
$array[3];

So that the array has only sequential numeric keys? Thanks!

Dharman
  • 30,962
  • 25
  • 85
  • 135
williamg
  • 2,738
  • 6
  • 34
  • 48

3 Answers3

8

array_values() returns all the values from the input array and indexes numerically the array.

John Carter
  • 53,924
  • 26
  • 111
  • 144
  • 1
    To get a index starting at one, you have to hack around after calling array_values: `$arr = array_unshift($arr, null); unset($arr[0]);` – Tyzoid Aug 21 '13 at 18:43
1

You can call array_values() on that array, and it will return a newly indexed array. (This assumes you have a numerically indexed array of course)

Dominic Barnes
  • 28,083
  • 8
  • 65
  • 90
0
$array = array_merge($array, array());

As long as $array has numerical keys, it will reorder them (starting from 0), numerically.

Tyler Carter
  • 60,743
  • 20
  • 130
  • 150