6

Example:

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

4 Answers 4

15

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

PHP docs for array_values

Sign up to request clarification or add additional context in comments.

2 Comments

You're welcome. Thank goodness for php and millions of array functions.
Brian, curse PHP and millions of array functions. ;)
4
array_values($arr);

Comments

1

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;
}

Comments

-1

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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.