I'm trying to increment during the map method, however my variable doesn't seem to save the new value. My code:
$position = 0;
$statement = $medias->map(fn(string $media) => [
'position' => ++$position
])->values();
My expected result would be:
#items: array:4 [
0 => array:4 [
"position" => 1
]
1 => array:4 [
"position" => 2
]
2 => array:4 [
"position" => 3
]
3 => array:4 [
"position" => 4
]
]
However it's actually
#items: array:4 [
0 => array:4 [
"position" => 1
]
1 => array:4 [
"position" => 1
]
2 => array:4 [
"position" => 1
]
3 => array:4 [
"position" => 1
]
]
Is incrementing inside map not possible or am I doing something wrong here?