在PHP中读取JSON以从日期字段创建字符串
我正在尝试从PHP中的JSON获取日期值:在PHP中读取JSON以从日期字段创建字符串,php,json,Php,Json,我正在尝试从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_t
{
"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”(最终)
问题在于您给出对象元素的错误。请参阅我的代码:
$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;
}
}}
$json=”{
“数据”:[
{
“开始”:{
“日期”:“2019-12-27 21:05:28.073000”,
“时区类型”:3,
“时区”:“UTC”
},
“最终”:{
“日期”:“2019-12-28 20:59:43.153000”,
“时区类型”:3,
“时区”:“UTC”
}
}
]
}';
$data=json_decode($json);
如果(计数($data->data)){
//打开桌子
//在阵列中循环
foreach($data->data as$idx=>$stand){
如果(设置($stand->start)){
回显“第一次操作”。$stand->start->date。“
”;
}否则{
回显“最终操作”。$stand->final->date;
}
}}
$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;
}
}}