[英]Change JSON_Encode output format (Brackets and Commas)
I have an array that is created in PHP, and then encoded into my javascript via JSON. 我有一个用PHP创建的数组,然后通过JSON编码到我的JavaScript中。
The Array is defined here: 数组在这里定义:
$stmt -> bind_result($match_id, $hero, $mmr);
while($stmt -> fetch()){
$grapharray[] = array($hero => $mmr);
}
and JSON encoded here: 和JSON编码在这里:
$grapharray_labelled = array(
"label" => "MMR Over time",
"data" => $grapharray
);
and here: 和这里:
var graphdata = <?php echo JSON_encode($grapharray_labelled); ?>;
graphdata
=: 当我运行网页时,输出为graphdata
=: {
"label":"MMR Over time",
"data":[
{"Rubick":6524},
{"Lion":6550},
{"Magnus":6565},
{"Keeper of the Light":6566}
]
}
{
"label":"MMR Over time",
"data":[
["Rubick", 6524],
["Lion", 6550],
["Magnus", 6565],
["Keeper of the Light", 6566]
]
}
Reason: I would like to change the format because I am trying to get flot to work, and flot accepts an array of arrays as the datatype. 原因:我想更改格式,因为我试图使flot正常工作,并且flot接受数组数组作为数据类型。
Otherwise: Is there a better way to transfer an array from PHP to JavaScript with my desired format? 否则:是否有更好的方法将数组从PHP转换为具有所需格式的JavaScript?
Change this: 更改此:
$grapharray[] = array($hero => $mmr);
TO: 至:
$grapharray[] = array($hero, $mmr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.