我正在尝试从 PHP 中的 JSON 获取日期值:{ "data": [ { "start": { "date": "2019-12-27 21:05:28.073000", "timezone_type": 3, "timezone": "UTC" }, "final": { "date": "2019-12-28 20:59:43.153000", "timezone_type": 3, "timezone": "UTC" } } ]}我的 PHP 代码正在尝试读取 JSON,但我想我没有达到开始和最终的子级别。我收到一个错误,没有返回任何内容。我试图进入一个变量“2019-12-27 21:05:28 UTC+3”(开始)和另一个变量“2019-12-28 20:59:43 UTC+3”(最终)<?php $json=file_get_contents("https://XXX/myjson.php");$data = json_decode($json);if (count($data->data)) { // Open the table // Cycle through the array foreach ($data->data as $idx => $stand) { echo "first operation ".$stand->start."<br>"; echo "final operation ".$stand->final; }}?>
1 回答
汪汪一只猫
TA贡献1898条经验 获得超8个赞
问题在于您错误地给出了对象元素。看我的代码:
$json='{
"data": [
{
"start": {
"date": "2019-12-27 21:05:28.073000",
"timezone_type": 3,
"timezone": "UTC"
},
"final": {
"date": "2019-12-28 20:59:43.153000",
"timezone_type": 3,
"timezone": "UTC"
}
}
]
}';
$data = json_decode($json);
if (count($data->data)) {
// Open the table
// Cycle through the array
foreach ($data->data as $idx => $stand) {
if(isset($stand->start)){
echo "first operation ".$stand->start->date."<br>";
}else{
echo "final operation ".$stand->final->date;
}
}}
- 1 回答
- 0 关注
- 82 浏览
添加回答
举报
0/150
提交
取消