用php分隔json标签和值

时间:2016-03-06 23:16:16

标签: 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:2016",
    "count": "380"
}]

1 个答案:

答案 0 :(得分:0)

PHP

$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

data.json文件(您当前的数据)

[{
    "01:03:2016": "410",
    "02:03:2016": "200",
    "03:03:2016": "380"
}]