29

I have an array:

Array
(
 [0] => ololo
 [2] => test
 [3] => haha
 [7] => nice
)

How can I change the indexes of the array to this:

Array
(
 [0] => ololo
 [1] => test
 [2] => haha
 [3] => nice
)
kopaty4
  • 2,146
  • 3
  • 24
  • 38
  • possible duplicate of [Making sure a PHP array has only sequential keys.](http://stackoverflow.com/questions/2173580/making-sure-a-php-array-has-only-sequential-keys) – Felix Kling Jun 17 '11 at 00:13
  • Possible duplicate of [How do you reindex an array in PHP?](https://stackoverflow.com/questions/591094/how-do-you-reindex-an-array-in-php) – Jannie Theunissen Jun 19 '18 at 14:09

5 Answers5

65

From PHP.net:

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

Source

$arr = array_values($arr);
Coded Monkey
  • 604
  • 7
  • 17
Paul DelRe
  • 3,925
  • 1
  • 22
  • 25
7

array_values() is probably what you want. See: http://php.net/function.array-values

$myArray = array_values($myArray);
Sean
  • 564
  • 1
  • 3
  • 12
3

This will re-index the array keys:

array_values($array)
user431949
  • 175
  • 1
  • 8
2

array_values()

timdev
  • 60,131
  • 6
  • 78
  • 90
2

If you have your initial array within $a variable, you can just do the following:

$a = array_values($a);

Which will basically return values from within your original array, and will do it in the another array.

Is it clear enough?

Tadeck
  • 125,377
  • 26
  • 148
  • 197