0

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?

stack
  • 9,490
  • 15
  • 57
  • 103

4 Answers4

2

You can use array_values():

$newarr = array_values($arr);

Or just overwrite the previous variable with the new array:

$arr = array_values($arr);
Xorifelse
  • 7,708
  • 1
  • 24
  • 37
2

just use the array_values function :

$newarr = array_values($arr);
Uri Goren
  • 12,532
  • 6
  • 50
  • 100
0

You can try this to achieve expected result

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

foreach ($arr as $index => $value) {
    $newarr[] = $value;
}
harigorana
  • 112
  • 1
  • 6
0
$a = 0;
foreach ($arr as $i=>$value) {
$newwarr[$a] = $value;
$a++;
}
vincent
  • 57
  • 2
  • 5