Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP fix numeric keys in array

For some reason when removing items from an array I am left with keys like 0, 2, 3, 4, 6, 9 instead of 0, 1, 2, 3, 4, 5. So I am trying to figure out why, and what I can do to fix it without sorting everything via sort() as that will put stuff in order. I just want to re-key in a matter of speaking.

like image 654
chris Avatar asked Jun 08 '12 23:06

chris


1 Answers

Use array_values() to get the values of the original array and return them to a new array. That new array will contain new numerical keys.

$new_array = array_values($old_array);
like image 157
John Conde Avatar answered Sep 22 '22 07:09

John Conde