tags:

views:

20

answers:

2

I just used array_filter to remove entries that had only the value '' from an array, and now I want to apply certain transformations on it depending on the placeholder starting from 0, but unfortunately it still retains the original index. I looked for a while and couldn't see anything, perhaps I just missed the obvious, but my question is...

how can I easily reset the indexes of the array to begin at 0 and go in order in the NEW array, rather than have it retain old indexes.

Thanks a million for any help.

+2  A: 

If you call array_values on your array, it will be reindexed from zero.

Daniel Vandersluis
So annoying that I have to wait to approve your answer.
jel402
A: 

Use array_values():

<?php

$array = array('foo', 'bar', 'baz');
$array = array_filter($array, function ($var) {
    return $var !== 'bar';
});

print_r($array); // indexes 0 and 2
print_r(array_values($array)); // indexes 0 and 1
Edward Mazur