I have an array like this ...
[0,0,23,0,0,18,0,0]
Then I want to change values that are not '0' (23 & 18) to auto increment, so the end result will be like this,
[0,0,1,0,0,2,0,0]
is there the best way for all that? So far this is what I did, but the results were not as expected ... :)
<?php
$arr = [0,0,23,0,0,18,0,0];
$x = 1;
$r = collect($arr)->map(function ($value, $key)use($x) {
if ($value == 0) {
return $value;
} else {
return $x++;
}
})->all();
dd($r);