Resort array after unset?

Is there a built-in that re-indexes an array if one of the elements were removed?


echo '<pre>';
$array = array('first','second','third','fourth','fith');
unset ($array[1]);
sort($array);
print_r($array);

gives me the output below. notice how it is out of “order.”


Array
(
    [0] => first
    [1] => fith
    [2] => fourth
    [3] => third
)

i know sort() is to sort it by alpha, but would like an output like this instead (re-sort based on index number)


Array
(
    [0] => first
    [1] => third
    [2] => fourth
    [3] => fith
)

[Edit]
n/m. i think i found it – ksort()

[Edit2]
arg! it didn’t re-index the list. ksort() didn’t do it

[Edit3]
Found it! array_values() will re-index the list.


echo '<pre>';
$array = array('first','second','third','fourth','fith');
unset ($array[1]);
$array = array_values($array);
print_r($array);


Array
(
    [0] => first
    [1] => third
    [2] => fourth
    [3] => fith
)

There’s nothing as satisfying as solving your own problem, is there? :slight_smile:

Owen