本文实例讲述了php简单构造json多维数组的方法。分享给大家供大家参考,具体如下:
php构造json多维数组其实很简单
下面列举了将mysqli的查询结果数组序列化为json的php代码如下
$res['result'] = "ok";
$res['msg'] = 'login';
$res['fileinfo'] = array();
while($stmt->fetch()){
$fileinfo['fileid'] = $fileid;
$fileinfo['name'] = $name;
$fileinfo['fujianname'] = $fujianname;
array_push($res['fileinfo'], $fileinfo);
}
echo json_encode($res);
输出的字符串如下:
[url=http://tools.jb51.net/code/json]http://tools.jb51.net/code/json[/url]
[b]JSON[/b][b]在线格式化工具:
[/b][url=http://tools.jb51.net/code/jsonformat]http://tools.jb51.net/code/jsonformat[/url]
[b]在线XML/JSON互相转换工具:
[/b][url=http://tools.jb51.net/code/xmljson]http://tools.jb51.net/code/xmljson[/url]
[b]json[/b][b]代码在线格式化/美化/压缩/编辑/转换工具:
[/b][url=http://tools.jb51.net/code/jsoncodeformat]http://tools.jb51.net/code/jsoncodeformat[/url]
[b]C语言风格/HTML/CSS/json代码格式化美化工具:
[/b][url=http://tools.jb51.net/code/ccode_html_css_json]http://tools.jb51.net/code/ccode_html_css_json[/url]
更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/239.htm]PHP中json格式数据操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/630.htm]PHP数学运算技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/348.htm]PHP基本语法入门教程[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/84.htm]php+mysql数据库操作入门教程[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》
希望本文所述对大家PHP程序设计有所帮助。