PHP按值分组-JSON [英] PHP Group By value - JSON
本文介绍了PHP按值分组-JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下JSON数据:
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"
}
我想将dateTime"start"拆分为$ date和$ time.它适用于此:
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>';
使用上面的代码,我看到下拉列表,其中所有值均被$ date分割.
With the above code I see the dropdown list with all values splitted by $date.
现在,我想按$ date分组,然后在另一个选择下拉列表中显示$ time分组.
Now I want to group by $date and then show $time grouped in another select dropdown.
请参阅方案: 图像方案
推荐答案
您需要使用php创建javascript数组.
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>
这篇关于PHP按值分组-JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文