tags:

views:

128

answers:

4

Example:

$arr = array(1 => 'Foo', 5 => 'Bar', 6 => 'Foobar');
/*... do some function so $arr now equals:
    array(0 => 'Foo', 1 => 'Bar', 2 => 'Foobar');
*/
+10  A: 

Use array_values($arr). That will return a regular array of all the values (indexed numerically).

PHP docs for array_values

Brian Ramsay
Perfect. Thank you Brian!
James Skidmore
You're welcome. Thank goodness for php and millions of array functions.
Brian Ramsay
Brian, curse PHP and millions of array functions. ;)
eyelidlessness
Ahh, too true...
Brian Ramsay
+4  A: 
array_values($arr);
skrebbel
A: 

Not that I know of, you might have already checked functions here

but I can imagine writing a simple function myself

resetarray($oldarray)
{
for(int $i=0;$i<$oldarray.count;$i++)
     $newarray.push(i,$oldarray[i])

return $newarray;
}

I am little edgy on syntax but I guess u got the idea.

satyajit
-1: Not only is the syntax wrong, but the logic is too. Not to mention that correct answers had been posted.
eyelidlessness
+1  A: 

To add to the other answers, array_values() will not preserve string keys. If your array has a mix of string keys and numeric keys (which is probably an indication of bad design, but may happen nonetheless), you can use a function like:

function reset_numeric_keys($array = array(), $recurse = false) {
    $returnArray = array();
    foreach($array as $key => $value) {
        if($recurse && is_array($value)) {
            $value = reset_numeric_keys($value, true);
        }
        if(gettype($key) == 'integer') {
            $returnArray[] = $value;
        } else {
            $returnArray[$key] = $value;
        }
    }

    return $returnArray;
}
eyelidlessness