在这里我正在使用php代码获取数据。但是我想将相同的代码转换为json格式。我没有办法做到这一点。我已经尝试过这种方式。
我的代码
while ($rows = mysql_fetch_array($filter)) {
$result['name'] = $rows['name'];
$result['id'] = $rows['id'];
}
$res = json_encode($result);
print_r($res);
得到结果
{"name":["sradha","nicky","demo","testing"],"id":["1","2","3","4"]}
在这里我想要下面的json格式
[{id:1, name:'sradha'},
{id:2, name:'nicky'},
{id:3, name:'demo'},
{id:3, name:'testing'}];
请给我建议。任何建议将不胜感激。先感谢您。
请尝试以下方法:
while ($rows = mysql_fetch_assoc($filter)) {
$result['name'] = $rows['name'];
$result['id'] = $rows['id'];
$new_result[] = $result;
}
$res = json_encode($new_result);
print_r($res);
或者
while ($rows = mysql_fetch_assoc($filter)) {
$result[] = $rows;
}
$res = json_encode($result);
print_r($res);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句