PHP Group per valore – JSON

Ho questi dati JSON:

"slots": [ { "start": "2017-02-09 08:00:00", "end": "2017-02-09 08:20:00" }, { "start": "2017-02-09 08:20:00", "end": "2017-02-09 08:40:00" }, { "start": "2017-02-09 08:40:00", "end": "2017-02-09 09:00:00" }, { "start": "2017-02-10 08:40:00", "end": "2017-02-10 09:00:00" }, { "start": "2017-02-10 09:00:00", "end": "2017-02-10 09:20:00" }, { "start": "2017-02-10 09:20:00", "end": "2017-02-10 09:40:00" } 

Vorrei dividere la dataTime "start" su $ date e $ time. Funziona con questo:

 echo '<select name="reservationDate" id="test">'; foreach($slots_start as $option){ $dateTime = $option->start; list($date, $time) = explode(" ", $dateTime); echo '<option value=' . $date. '>' . $date . '</option>'; } echo '</select>'; 

Con il codice sopra vedo l'elenco a discesa con tutti i valori suddivisi in $ date.

Ora voglio raggruppare per $ date e quindi mostrare $ time raggruppato in un altro menu a discesa Seleziona.

Vedi Schema: schema image

Avrai bisogno di creare javascript arrays con php.

https://jsfiddle.net/bruvygjf/

 <?php $json_str =<<<EOF [ { "start": "2017-02-09 08:00:00", "end": "2017-02-09 08:20:00" }, { "start": "2017-02-09 08:20:00", "end": "2017-02-09 08:40:00" }, { "start": "2017-02-09 08:40:00", "end": "2017-02-09 09:00:00" }, { "start": "2017-02-10 08:40:00", "end": "2017-02-10 09:00:00" }, { "start": "2017-02-10 09:00:00", "end": "2017-02-10 09:20:00" }, { "start": "2017-02-10 09:20:00", "end": "2017-02-10 09:40:00" } ] EOF; $slots = json_decode($json_str); $times = arrays(); foreach($slots as $option){ list($date, $time) = explode(" ", $option->start); if (!arrays_key_exists($date, $times)){ $times[$date] = arrays(); } if (!in_arrays($time, $times[$date])){ $times[$date][] = $time; } } echo '<select name="reservationDate" id="firstSelect" onchange="fillSecond()">'; foreach (arrays_keys($times) as $date){ echo ' <option value="' . $date. '">' . $date . '</option>'; } echo '</select>'; echo '<select name="reservationTime" id="secondSelect"></select>'; ?> <script language="javascript"> function fillSecond(){ firstSelectElement = document.getElementById('firstSelect'); secondSelectElement = document.getElementById('secondSelect'); times = date_options[firstSelectElement.selectedIndex]; while (secondSelectElement.options.length > 0) { secondSelectElement.remove(0); } for(var i = 0; i < times.length; i++) { var new_option = document.createElement('option'); new_option.innerHTML = times[i]; new_option.value = times[i]; secondSelectElement.appendChild(new_option); } } date_options = []; <?php foreach($times as $date => $times){ echo "date_options.push([]);"; foreach ($times as $time){ echo "date_options[date_options.length - 1].push('$time');"; } } ?> fillSecond(); </script> 

È ansible innanzitutto raggruppare i dati e quindi compilare entrambi i menu a discesa.

 <?php // group data $grouped = []; foreach($slots_start as $option){ $dateTime = $option->start; list($date, $time) = explode(" ", $dateTime); if (isset($grouped[$date])) { $grouped[$date][] = $time; } else { $grouped[$date] = [$time]; } } 

Quindi visualizza entrambi i menu a discesa.

È ansible get la matrix di date con

 $dates = arrays_keys($grouped); 

E each volta in relazione a quella data con:

 $times = $grouped[$date]; 

In questo modo otterrai un codice molto più maneggevole. Spero che sia d'aiuto.

php con:

 <select name="company" id="select_company"> <option>Select...</option> <option value="1">Italy</option> </select> <select name="reservationDate" id="firstSelect"> <option>Seleziona date</option> </select> //ajax $('#select_company').on('change', function(e) { e.preventDefault(); // Prevent Default Submission $.ajax({ url: 'my_page.php', type: 'POST', data: $(this).serialize() }) .done(function(data){ $('#firstSelect').hide(function(){ $('#firstSelect').show().html(data); }); }) .fail(function(){ alert('Errore nel caricamento'); }); }); 

E my_page.php con:

  <?php if(isset($_POST)) { ...//my code for get json arrays $times = arrays(); foreach($slots_start as $option){ list($date, $time) = explode(" ", $option->start); if (!arrays_key_exists($date, $times)){ $times[$date] = arrays(); } if (!in_arrays($time, $times[$date])){ $times[$date][] = $time; } } echo '<select name="reservationDate" id="firstSelect" onchange="fillSecond()">'; foreach (arrays_keys($times) as $date){ echo ' <option value="' . $date. '">' . $date . '</option>'; } echo '</select>'; echo '<select name="reservationTime" id="secondSelect"></select>'; ?> <script language="javascript"> function fillSecond(){ firstSelectElement = document.getElementById('firstSelect'); secondSelectElement = document.getElementById('secondSelect'); times = date_options[firstSelectElement.selectedIndex]; while (secondSelectElement.options.length > 0) { secondSelectElement.remove(0); } for(var i = 0; i < times.length; i++) { var new_option = document.createElement('option'); new_option.innerHTML = times[i]; new_option.value = times[i]; secondSelectElement.appendChild(new_option); } } date_options = []; <?php foreach($times as $date => $times){ echo "date_options.push([]);"; foreach ($times as $time){ echo "date_options[date_options.length - 1].push('$time');"; } } ?> fillSecond(); </script> <?php }?>