我遇到一个小问题,我正在使用PHP显示json,我有这个代码:
foreach($result as $r){
$returnEcho["Id"] = $r["id"];
$returnEcho["Username"] = $r["username"];
$returnEcho["Email"] = $r["email"];
$returnEcho["Info"] = $r["Info"];
echo json_encode($returnEcho);
此代码的问题是,它将显示如下的JSON:
{"Username":"X","Email":"X","Info":"X"}
{"Username":"X","Email":"X","Info":"X"}
{"Username":"X","Email":"X","Info":"X"}
但我想要的是这样的:
[
{
"Username":"X",
"Email":"X",
"Info":"X"
},
{
"Username":"X",
"Email":"X",
"Info":"X"
}
]
我该怎么做? 感谢。
答案 0 :(得分:4)
只有在echo
完成后才需要在数组中构建所有内容并{j} {j}。像这样:
foreach