stack stack - 1 year ago 128
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 Source

You can use:

$newarr = array_values($arr);

Or just overwrite the previous variable with the new array:

$arr = array_values($arr);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download