使用php分离json标签和值
我已在我的网站上访问了此json文件:使用php分离json标签和值,php,json,Php,Json,我已在我的网站上访问了此json文件: [{ "01:03:2016": "410", "02:03:2016": "200", "03:03:2016": "380" }] 我想使用PHP将格式更改为这样,其中日期和计数都是值: [{ "date": "01:03:2016", "count": "410" }, { "date": "02:03:2016", "count": "200" }, { "date": "03:03
[{
"01:03:2016": "410",
"02:03:2016": "200",
"03:03:2016": "380"
}]
我想使用PHP将格式更改为这样,其中日期和计数都是值:
[{
"date": "01:03:2016",
"count": "410"
},
{
"date": "02:03:2016",
"count": "200"
},
{
"date": "03:03:2016",
"count": "380"
}]
PHP
data.json文件(您当前的数据)
在foreach循环中,当我删除$json_a之后的[0]时,它就起作用了。
$string = file_get_contents("data.json"); // your current json data
$json_a = json_decode($string, true);
$new_json = array();
foreach ($json_a as $key => $value) {
$new_json[] = array("date"=>$key,'count'=>$value);
}
echo json_encode($new_json); //output
[{
"01:03:2016": "410",
"02:03:2016": "200",
"03:03:2016": "380"
}]