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!

3
  • do you want to renumber the keys starting at zero? If so, therefromhere's answer is perfect. Commented Jan 31, 2010 at 22:16
  • Well, all the answers will work for starting for zero. If you just want them to be sequential (not starting from 0), none of the answers will work. Commented Jan 31, 2010 at 22:17
  • @Chacha102 You can get an index at one, but it's a pretty hacky solution. Commented Aug 21, 2013 at 18:43

3 Answers 3

8

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

Sign up to request clarification or add additional context in comments.

1 Comment

To get a index starting at one, you have to hack around after calling array_values: $arr = array_unshift($arr, null); unset($arr[0]);
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)

Comments

0
$array = array_merge($array, array());

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.