Echo a json with Php 添加字符
Echo a json with Php adding characters
我正在尝试生成 jqplot 图表。我有一个简单的问题,但我找不到任何解决方案,如果你能帮助我,我将不胜感激,如果我也能的话,我也会 return,嗨嗨。
Jqplot Graph需要这样的JSON数据(开头和结尾各有3个“[”字符):
[[["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 直接显示在网络导航器中的行!
我想创建一个主 php 数组,用于包含三个“[”和“]”,但我不知道该怎么做?
我试过了:
echo('['.json_encode($tabGen).']');
但这也行不通,哼!
所以我需要的是直接进入我的导航器的这一行:
[[["Sciences",5],["Lettres",2],["Informatique",1],["Commerce",1],["Technologie 1",1]]]
如果您能帮助我,我将不胜感激,如果可以的话,我会在 return 中解决 javascript jquery 问题或 jqplot 提示。
谢谢你有一个 niiiiiice 的一天 "hi hi" !啊啊
编辑:没关系,我刚刚找到了解决方案
$tabAdded=json_encode($tabGen);
echo('['.$tabAdded.']');
然后,该行正确打印了 3 个“[[[”和 3 个“]]]”,谢谢
希望我做对了,你想在包含数组的数组中输出数组。
没有魔法,只需用另一个数组包裹您的数组即可获得所需的结果。
header('content-type', 'application/json');
echo json_encode([$a]);
在json_encode
之前再添加一个数组级
file_put_contents('g1.json', json_encode([$tabGen]));
我正在尝试生成 jqplot 图表。我有一个简单的问题,但我找不到任何解决方案,如果你能帮助我,我将不胜感激,如果我也能的话,我也会 return,嗨嗨。
Jqplot Graph需要这样的JSON数据(开头和结尾各有3个“[”字符):
[[["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 直接显示在网络导航器中的行!
我想创建一个主 php 数组,用于包含三个“[”和“]”,但我不知道该怎么做?
我试过了:
echo('['.json_encode($tabGen).']');
但这也行不通,哼!
所以我需要的是直接进入我的导航器的这一行:
[[["Sciences",5],["Lettres",2],["Informatique",1],["Commerce",1],["Technologie 1",1]]]
如果您能帮助我,我将不胜感激,如果可以的话,我会在 return 中解决 javascript jquery 问题或 jqplot 提示。 谢谢你有一个 niiiiiice 的一天 "hi hi" !啊啊
编辑:没关系,我刚刚找到了解决方案
$tabAdded=json_encode($tabGen);
echo('['.$tabAdded.']');
然后,该行正确打印了 3 个“[[[”和 3 个“]]]”,谢谢
希望我做对了,你想在包含数组的数组中输出数组。
没有魔法,只需用另一个数组包裹您的数组即可获得所需的结果。
header('content-type', 'application/json');
echo json_encode([$a]);
在json_encode
之前再添加一个数组级file_put_contents('g1.json', json_encode([$tabGen]));