在这里我正在使用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);
标签:json,php
来源: https://codeday.me/bug/20191027/1943468.html