热搜:NVER node 开发 php

mysql to json的问题

2024-09-21 10:40:01
mysql to json的问题

我想生成嵌套的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);