本文实例讲述了PHP生成及获取JSON文件的方法。分享给大家供大家参考,具体如下:
首先定义一个数组,然后遍历数据表,把相应的数据放到数组中,最后通过json_encode()转化数组
json_encode() 函数的功能是将数值转换成 JSON 数据存储格式。
putjson.php:
<?php
// 生成一个PHP数组
$data = array();
$data[0] = array('1','吴者然','onestopweb.cn');
$data[1] = array('2','何开','iteye.com');
// 把PHP数组转成JSON字符串
$json_string = json_encode($data);
// 写入文件
file_put_contents('test.json', $json_string);
?>
有同名的 JSON 文件则覆盖,没有则创建。
生成或覆盖的 JSON 如下:
[url=http://tools.jb51.net/code/json]http://tools.jb51.net/code/json[/url]
JSON在线格式化工具:
[url=http://tools.jb51.net/code/jsonformat]http://tools.jb51.net/code/jsonformat[/url]
在线XML/JSON互相转换工具:
[url=http://tools.jb51.net/code/xmljson]http://tools.jb51.net/code/xmljson[/url]
json代码在线格式化/美化/压缩/编辑/转换工具:
[url=http://tools.jb51.net/code/jsoncodeformat]http://tools.jb51.net/code/jsoncodeformat[/url]
C语言风格/HTML/CSS/json代码格式化美化工具:
[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/234.htm]PHP针对XML文件操作技巧总结[/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程序设计有所帮助。