问题
I have this JSON data:
"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"
}
I would like to split dateTime "start" to $date and $time. It work with this:
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>';
With the above code I see the dropdown list with all values splitted by $date.
Now I want to group by $date and then show $time grouped in another select dropdown.
See Scheme: image scheme
回答1:
You'll need to create javascript array with 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 = array();
foreach($slots as $option){
list($date, $time) = explode(" ", $option->start);
if (!array_key_exists($date, $times)){
$times[$date] = array();
}
if (!in_array($time, $times[$date])){
$times[$date][] = $time;
}
}
echo '<select name="reservationDate" id="firstSelect" onchange="fillSecond()">';
foreach (array_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>
回答2:
You could group your data first, and then populate both dropdowns.
<?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];
}
}
Then output both dropdowns.
You can get the dates array with
$dates = array_keys($grouped);
And every time related to that date with:
$times = $grouped[$date];
That way you will get much more mantainable code. Hope it helps.
回答3:
php with:
<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');
});
});
And my_page.php with:
<?php
if(isset($_POST))
{
...//my code for get json array
$times = array();
foreach($slots_start as $option){
list($date, $time) = explode(" ", $option->start);
if (!array_key_exists($date, $times)){
$times[$date] = array();
}
if (!in_array($time, $times[$date])){
$times[$date][] = $time;
}
}
echo '<select name="reservationDate" id="firstSelect" onchange="fillSecond()">';
foreach (array_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
}?>
来源:https://stackoverflow.com/questions/42115127/php-group-by-value-json