Replace PHP array value with current day
I have this array:
$flights = Array (
[0] => Array (
[day] => 0
[flights] => 0
)
[1] => Array (
[day] => 0
[flights] => 0
)
[2] => Array (
[day] => 0
[flights] => 0
)
[3] => Array (
[day] => 0
[flights] => 0
)
[4] => Array (
[day] => 0
[flights] => 0
)
[5] => Array (
[day] => 0
[flights] => 0
)
[6] => Array (
[day] => 0
[flights] => 0
)
[7] => Array (
[day] => 0
[flights] => 0
)
[8] => Array (
[day] => 0
[flights] => 0
)
[9] => Array (
[day] => 0
[flights] => 0
)
[10] => Array (
[day] => 0
[flights] => 0
)
[11] => Array (
[day] => 0
[flights] => 0
)
[12] => Array (
[day] => 0
[flights] => 0
)
)
And I want to replace all [day]
values with the current correspondent day, like:
Array (
[0] => Array (
[day] => 01
[flights] => 0
)
[1] => Array (
[day] => 02
[flights] => 0
)
[2] => Array (
[day] => 03
[flights] => 0
)
...
The code is creating the array key numbers according to the current day of the month, so the array has the correct number of keys for today (13/01).
What is the function to do this?
Solution 1:
Better way is to do:
foreach ($flights as $key => $_) {
$flights[$key]['day'] = $key + 1;
}
Here we're looping through all the flights, utilizing its numbered $key
, incrementing it by 1
and assigning it to the appropriate place.