php - 如何使用 php 从 foreach 循环中检索 json_encode 数据
问题描述
在一个目录中,我有很多 json 文件,如下所示:
{
"id": "id_11-08-2021",
"name": "Jack",
"date": "11-08-2021",
"day": "Woensdag",
"starttime": "08:30",
"endtime": "10:00",
"hours": 1.5
}
通过 ajax,我尝试从所有这些文件中接收数据:
$.ajax({
type: "POST",
url: "process.php",
data: {
show_hours: 1,
name: name
},
processData: false,
dataType: "json",
success: function(response) {
if(response.status == 2) {
$('tbody').html(response.data); // output in table body
}
},
error: function() {
alert('Something went wrong!');
}
});
process.php
看起来像这样:
$result = [];
if( isset($_POST['show_hours']) ) {
$files = glob('data/'.$year.'/'.$month.'/'.$name.'/*.json'); // all json files
foreach($files as $file) {
$objs[] = json_decode(file_get_contents($file),1); // all json objects in array
}
foreach($objs as $key => $val) {
$result['data'] = array(
'tr' => '<tr>';
'id' => '<td class="d-none">'.$val['id'].'</td>';
'date' => '<td>'.$val['date'].'</td>';
'day' => '<td>'.$val['day'].'</td>';
'starttime' => '<td>'.$val['starttime'].'</td>';
'endime' => '<td>'.$val['endtime'].'</td>';
'hours' => '<td class="calc-hours">'.$val['hours'].'</td>';
'tr' => '</tr>';
);
}
$result['status'] = 2; // status 2 for show data by name
echo json_encode($result);
但我收到了警报Something went wrong!
我做错了什么?
解决方案
推荐阅读
- azure - 通过 Azure DevOps VSTS 部署 ARM 模板时出现无法确定的错误
- javascript - 监视具有多个参数和回调的方法
- java - MongoDB POJO 和内部/嵌套类
- streaming - 流媒体协议
- python - 使用 tkinter 条目验证防止用户两次输入相同的值
- android - MVP 架构
- sql - 您如何并排“加入”多个 SQL 数据集(彼此不链接)?
- javascript - 为什么无阻塞节点会减少 I/O 总时间?
- scala - circe 自动推导 - 与进口作斗争
- javascript - 在 url (Javascript) 上添加部分时无法识别 Cookie