形成json响应

时间:2015-08-05 04:45:04

标签: php json

这是我的JSON回复:

[{"id":"1","company":"india"},{"id":"2","company":"australia"},{"id":"3","company":"srilanka"},{"id":"4","company":"indonasia"}]

我希望它是

{"results:"[{"id":"1","company":"india"},{"id":"2","company":"australia"},{"id":"3","company":"srilanka"},{"id":"4","company":"indonasia"}] }

我该怎么做?

PHP代码:

$q= mysqli_query($conn,"SELECT * FROM world");

     while($e = mysqli_fetch_assoc($q))  
             $output[]=$e;  

     print(json_encode($output));

3 个答案:

答案 0 :(得分:0)

简单$output['result']=$e

答案 1 :(得分:0)

应为$output['result'][]=$e;

$output = array();
while ($e = mysqli_fetch_assoc($q))
{
    $output['results'][] = $e;
}
print(json_encode($output)); 

答案 2 :(得分:0)

您不需要使用while。试试这个:

$q= mysqli_query($conn,"SELECT * FROM world");
$e = mysqli_fetch_assoc($q);
$result = array();
$result['result'] = $e;
print(json_encode($result));