31

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,236
  • 4
  • 26
  • 39

5 Answers5

71

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
  • 19
Paul DelRe
  • 4,003
  • 1
  • 24
  • 26
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
  • 165
  • 1
  • 8
2

array_values()

timdev
  • 61,857
  • 6
  • 82
  • 92
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
  • 132,510
  • 28
  • 152
  • 198