<?php
$json = '{"name":"Test","id_test":8979,"availability":[{"dateStarts":"2019-12-14","dateEnds":"2019-12-21","dateFormat":"Dec 14 - 21, 2019","length":7,"spaces":5,"program":{"id":2,"title":"Long 2/2","program":"WESTERN ISLANDS"},"rate":{"id":2,"title":"Western Islands Program","prices":[{"custom":true,"defaultPrice":"4845","price":"4545","deck":"all cabins & decks","season":"","cabin":""}]}},{"dateStarts":"2019-12-21","dateEnds":"2019-12-28","dateFormat":"Dec 21 - 28, 2019","length":7,"spaces":"-1","program":{"id":1,"title":"Long 1/2","program":"EASTERN ISLANDS"},"rate":{"id":2,"title":"Eastern Islands Program","prices":[{"custom":false,"defaultPrice":"4845","price":"4845","deck":"all cabins & decks","season":"","cabin":""}]}},{"dateStarts":"2019-12-28","dateEnds":"2020-01-04","dateFormat":"Dec 28 - Jan 04 2020","length":7,"spaces":"-1","program":{"id":2,"title":"Long 2/2","program":"WESTERN ISLANDS"},"rate":{"id":2,"title":"Western Islands Program","prices":[{"custom":false,"defaultPrice":"4845","price":"4845","deck":"all cabins & decks","season":"","cabin":""}]}}]}';
$request_data = json_decode( $json );
usort($request_data->availability, function ($a, $b) { return strcmp($a->program->program, $b->program->program); });
echo '<select class="select_availability" name="availability">' . "\n";
$last_program = '';
foreach($request_data->availability as $ava){
if($ava->rate->id == 2){
if ($ava->program->program != $last_program) {
if ($last_program != '') echo "</optgroup>\n";
echo '<optgroup label="' . $ava->program->program . '">' . "\n";
$last_program = $ava->program->program;
}
echo "<option>$ava->dateFormat</option>\n";
}
}
if ($last_program != '') echo "</optgroup>\n";
echo "</select>\n";
preferences:
64.37 ms | 406 KiB | 5 Q