Home  >  Article  >  Backend Development  >  mysql to json的问题

mysql to json的问题

WBOY
WBOYOriginal
2016-06-23 14:18:59858browse

我想生成嵌套的json的格式不知道怎么做。。。
比方说我要查询用户=aa的数据,可能不止一行:$row=mysql_fetch_array($rs);
结果可能是多行的: {{id=1,account=123,depart=xxx},{id=22,account=323,depart=kkk}}
我希望生成这样的json格式:
{user:aa,[{id=1,account=123,depart=xxx},{id=22,account=323,depart=kkk}]}应该怎么做?


回复讨论(解决方案)

数组拼凑起来不就可以了

数组拼凑起来不就可以了
howto?

格式应该是这样的:
{user:aa,result:[{id=1,account=123,depart=xxx},{id=22,account=323,depart=kkk}]}
字符串的双引号我省略了。

$ar=array();$ar['user']='aa';while($row=mysql_fetch_array($rs)){    $ar['result'][]=$row;}echo json_encode($ar);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn