我有此数组:PHP数组纠正JSON格式
$users = Array
(
[Gareth] => Array
(
[25732] => 180
[25689] => 2310
[25760] =>
[25759] =>
[25758] =>
[25728] =>
[25734] =>
)
[Adam] => Array
(
[25732] =>
[25689] =>
[25760] =>
[25759] =>
[25758] => 420
[25728] => 60
[25734] =>
)
[Cennydd ] => Array
(
[25732] =>
[25689] =>
[25760] =>
[25759] =>
[25758] =>
[25728] =>
[25734] => 1035
)
)
它有工作,他们已经花了上以分钟为单位的工作时间的IDS用户。
我需要使用highcharts
输出此数据,因此它需要使用Json格式。
目前,使用json_encode
,它返回:
{ "Gareth":
{
"25732":180,"25689":2310,"25760":null,"25759":null,"25758":null,"25728":null,"25734":null
},
"Adam Jukes":
{
"25732":null,"25689":null,"25760":null,"25759":null,"25758":420,"25728":60,"25734":null
},
"Cennydd":
{
"25732":null,"25689":null,"25760":null,"25759":null,"25758":null,"25728":null,"25734":1035
}
}
但我需要这个格式:
[{
"name": 'Gareth ',
"data": [180, 2310,null, null, null,null, null]
},
{ "name": 'Adam',
"data": [null, null, null,null,420, 60, null]
},{
"name": 'Cennydd',
"data":[null, null , null, null, null,null, 1035 ]
}]
}]
但我似乎无法弄清楚。 我是否需要使用foreach
以正确的格式分隔并创建新阵列?
? –
您必须将php数组转换为所需的格式,然后将其编码为JSON – hindmost
您必须遍历数据并从中重新格式化一个新数组......还有什么? –