2015-02-11 23 views
0

我正在嘗試生成jqplot圖。我有一個簡單的問題,但我無法找到任何解決方案,如果你能幫助我,我將不勝感激,如果我也能做到,我也會回報,嗨嗨。用php添加字符回顯json

Jqplot格拉夫需要這種JSON數據(3「[」字符都在beggining和結束時):

[[["Sciences",5],["Lettres",2],["Informatique",1],["Commerce",1],["Technologie 1",1]]] 

這是工作PHP代碼我用於生成它(json_encode除()部分解釋如下):

$lectureSql5="SELECT distinct count(professeur.idcategcours) as decompte,categcours.nom as nom FROM professeur INNER JOIN categcours ON professeur.idcategcours=categcours.idcategcours group by professeur.idcategcours"; 
$lecture5 = mysql_query($lectureSql5) or die(mysql_error()); 

while ($col=mysql_fetch_array($lecture5)){ 
    $col[0]=intval($col[0]); 
    $tabGen[]=array($col[1],$col[0]); 
}; 



echo json_encode($tabGen); 


file_put_contents('g1.json', '['.json_encode($tabGen).']'); 

正如你所看到的,在陣列創建後,我嘗試json_encode和我得到這個我的html頁面:

[["Sciences",5],["Lettres",2],["Informatique",1],["Commerce",1],["Technologie 1",1]] 

這是一個很大的問題,因爲我錯過了在開始和結束時的三個「[」字符!所以我設法做到了file_put_contents('g1.json','['.json_encode($ tabGen)''''';')。它的工作原理,但它不是我所需要的,它創建一個文件!

我需要該行直接顯示json_encode到Web導航器中!

我想過創建一個主要的PHP數組,因爲有三個「[」和「]」但我不知道該怎麼做?

我已經試過:

echo('['.json_encode($tabGen).']'); 

但是,這並不工作過,SNIF!

所以我需要的是直接將此行我的導航:

[[["Sciences",5],["Lettres",2],["Informatique",1],["Commerce",1],["Technologie 1",1]]] 

我會grealty感激,如果你能幫助我,我會如果我能回報的javascript jquery的問題或jqplot提示。 謝謝你有一個niiiiiice日「嗨嗨」!啊

編輯:其確定我只是找到了解決辦法

$tabAdded=json_encode($tabGen); 

echo('['.$tabAdded.']'); 

於是,線路正常打印3 「[[[」 和3 「]]」 謝謝

+3

之前添加另一個陣列級不要試圖用手來構建JSON。用你想要的整個結構創建一個數組,然後在其上使用'json_encode()'。 – Barmar 2015-02-11 23:17:24

+0

非常感謝!是的,我想創建一個主數組(例如叫做$ main),之後推入$ tabGen,最後是json_encode($ main),但這並不容易,我嘗試過push( ),但這不是我真正需要的,而且我很難做到。 – 2015-02-11 23:21:49

+0

沒關係!剛剛找到解決方案!可能是因爲我描述了這個問題。只需添加 $ tabAdded = json_encode($ tabGen); echo('['。$ tabAdded。']'); – 2015-02-11 23:27:20

回答

0

我希望我沒錯,你想要在包含數組的數組中輸出數組。

沒有什麼魔力,只需將你的數組包裝到另一個數組即可獲得所需的結果。

header('content-type', 'application/json'); 
echo json_encode([$a]); 
0

json_encode

file_put_contents('g1.json', json_encode([$tabGen])); 
+0

非常感謝。我如何標記我的東西作爲解決?我找不到按鈕。 – 2015-02-11 23:34:45

+0

http://stackoverflow.com/tour – Blablaenzo 2015-02-11 23:43:53