从MySQL查询格式化JSON PHP

从MySQL查询格式化JSON PHP,php,json,post,get,Php,Json,Post,Get,我有一个关于在php中格式化json的问题。这是我的密码 公共功能测试(){ } 在我当前的简单php代码中,我只是将所有内容都作为JSONArray [ ] 但我想用我自己的方式格式化它,像这样的…希望你们能帮我。提前谢谢你 { “名单”:[ }试试这个: $list=$q->result(); $result=数组(“应答”=> 数组(“成功”=>“是”), “列表”=>$List ); 打印json_encode($result); 将打印: {"Answer":{"Sucess":"

我有一个关于在php中格式化json的问题。这是我的密码

公共功能测试(){

}

在我当前的简单php代码中,我只是将所有内容都作为JSONArray

[

]

但我想用我自己的方式格式化它,像这样的…希望你们能帮我。提前谢谢你

{

“名单”:[

}试试这个:


$list=$q->result();
$result=数组(“应答”=>
数组(“成功”=>“是”),
“列表”=>$List
);
打印json_encode($result);

将打印:

{"Answer":{"Sucess":"Yup"},"List":[{"id":1,"Description":"hello"}]}
  {"ID":1,"Description":"hello",

  {"ID":2,"Description":"hellou"}
"Answer": {

    "Success": "Yup"
          },
    {"ID":1,    
    "Description":"hello"},

    {"ID":2,    
    "Description":"hellou"}]
$result = array(
  'Answer' => array('Sucess'=>'Yup'),
  'List' => array(
     array('id' => 1, 'Description' => 'hello'),
  )
);
print json_encode($result);
{"Answer":{"Sucess":"Yup"},"List":[{"id":1,"Description":"hello"}]}