stack stack - 1 month ago 12
PHP Question

How to restart array keys?

I have an array like this:

$arr = array("2"=>"red", "5"=>"blue", "3"=>"black", "12"=>"orange");


Now I need to rewrite all array items. So this is excepted output:

$newarr = array("0"=>"red", "1"=>"blue", "2"=>"black", "3"=>"orange");


Is doing that possible?

Answer

You can use:

$newarr = array_values($arr);

Or just overwrite the previous variable with the new array:

$arr = array_values($arr);