PHP Echo json_encode

时间:2016-04-05 13:55:23

标签: php json

我遇到一个小问题,我正在使用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"
 }
]

我该怎么做? 感谢。

1 个答案:

答案 0 :(得分:4)

只有在echo完成后才需要在数组中构建所有内容并{j} {j}。像这样:

foreach