簡體   English   中英

更改JSON_Encode輸出格式(括號和逗號)

[英]Change JSON_Encode output format (Brackets and Commas)

我有一個用PHP創建的數組,然后通過JSON編碼到我的JavaScript中。

數組在這里定義:

$stmt -> bind_result($match_id, $hero, $mmr);
while($stmt -> fetch()){
    $grapharray[] = array($hero => $mmr);
}

和JSON編碼在這里:

$grapharray_labelled = array(
    "label" => "MMR Over time",
    "data" => $grapharray          
);

和這里:

var graphdata = <?php echo JSON_encode($grapharray_labelled); ?>;

當我運行網頁時,輸出為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]
        ]
}

原因:我想更改格式,因為我試圖使flot正常工作,並且flot接受數組數組作為數據類型。

否則:是否有更好的方法將數組從PHP轉換為具有所需格式的JavaScript?

更改此:

$grapharray[] = array($hero => $mmr);

至:

$grapharray[] = array($hero, $mmr);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM