Цикл сохраняет только последнее встреченное значение ⇐ Php
Цикл сохраняет только последнее встреченное значение
I am attempting to produce a single string with 2 values dates and cost
If a user selects a start date of 1/1/2025 and an end date of 3/1/2025 and the cost is 250 I am looking to build a string as follows.
"20250101":250,"20250102":250 I am using the following code.
$pricepern = 250; $period = new \DatePeriod( new \DateTime($start_date), new \DateInterval('P1D'), new \DateTime($end_date)); foreach ($period as $key => $value) { $individualdays = '"'.$value->format('Ymd').'":'.$pricepern.','; echo $individualdays; // It works in here } echo $individualdays; // It only provides the last value out here Would appreciate any help in understanding how I produce a single string outside of the for each. I did try implode outside however it indicated that the value was not an array.
Источник: https://stackoverflow.com/questions/780 ... ered-value
I am attempting to produce a single string with 2 values dates and cost
If a user selects a start date of 1/1/2025 and an end date of 3/1/2025 and the cost is 250 I am looking to build a string as follows.
"20250101":250,"20250102":250 I am using the following code.
$pricepern = 250; $period = new \DatePeriod( new \DateTime($start_date), new \DateInterval('P1D'), new \DateTime($end_date)); foreach ($period as $key => $value) { $individualdays = '"'.$value->format('Ymd').'":'.$pricepern.','; echo $individualdays; // It works in here } echo $individualdays; // It only provides the last value out here Would appreciate any help in understanding how I produce a single string outside of the for each. I did try implode outside however it indicated that the value was not an array.
Источник: https://stackoverflow.com/questions/780 ... ered-value
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Массив, построенный в цикле, сохраняет только последнее встреченное значение [дубликат]
Anonymous » » в форуме Php - 0 Ответы
- 23 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Foreach() сохраняет только последнее значение в массив результатов [дубликат]
Anonymous » » в форуме Php - 0 Ответы
- 19 Просмотры
-
Последнее сообщение Anonymous
-