{ "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }, { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" }, { "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }],"authors": [ { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" }, { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" }, { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" } ], "musicians": [ { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" }, { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" } ] } 这样的数据格式是怎么样转换的我从数据库查出来的数据直接转换的话是[{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }]这样的!
2 回答
慕的地10843
TA贡献1785条经验 获得超8个赞
其实就是把这3个结果并成一个数组,再转就是了
例如,
$res1=mysql_query($sql1);
$res2=mysql_query($sql2);
$res3=mysql_query($sql3);
定义一个新的数组 $a = array();
将3个结果压入这个新的数组
array_push($a,$res1);
array_push($a,$res2);
array_push($a,$res3);
最后转义输出
echo json_encode($a);
LZ遇到问题不要气馁呀
喵喔喔
TA贡献1735条经验 获得超5个赞
json和php数组 格式的互相转换,参考如下:
$php_json = json_encode($json_arr); //把php数组格式转换成 json 格式的数据
echo $php_json;
$php_json = json_decode($php_json); //再把json格式的数据转换成php数组
print_r($php_json);
json数组
$json = '[{"id":"22","name":"33","descn":"44"}]'; //json格式的数组转换成 php的数组
$arr = (Array)json_decode($json);
echo $arr[0]->id; //用对象的方式访问
添加回答
举报
0/150
提交
取消