用file_get_contents()和file_put_contents()向txt文档中存取数组
来源:互联网 发布:linux dd打包系统 编辑:程序博客网 时间:2024/04/30 02:29
举例说明,比如我们现在有这样的需求:学生给老师投票,老师的名字作为键,对应的值为给此老师投票的学生名字,多个学生名字间用“,”分隔,将所得数组序列化写入txt文档,下次投票的时候从txt文档中读出字符串再反序列化成数组,继续添加。
逐行说明:
1、假设现在学生李明给王老师投票;
2、如果ballot.txt存在则读取内容,反序列化赋值给数组$studentArr,注意这个很有用的函数file_get_contents(),可以自己完成fopen(),fread(),fclose()多步操作并且执行效率高,详见php手册;
3、如果数组有“王老师”这个键,将此键对应的值这个字符串按“,”分割为数组并赋值给$studentArr,注意array_key_exists()这个函数,详见php手册;
4、如果数组$studentArr中没有“李明”,则向数组$readArray中键为“王老师”对应的值中加上“李明”,并以“,”分隔;
5、否则不做添加;
6、将所得数组序列化写入ballot.txt,注意file_put_contents()函数的用法,详见php手册;
7、若想统计每个老师的得票数,请点击此链接http://blog.csdn.net/linglongwunv/archive/2010/01/12/5183164.aspx。
心情好,送大家两个比较有意思的php内置函数,将数组的键值互换:array_flip(),去掉数组中重复的值:array_unique(),详见php手册。
- 用file_get_contents()和file_put_contents()向txt文档中存取数组
- 关于php 中file_put_contents 和fwrite file_get_contents和curl
- 读取文件和下载文件file_get_contents,file_put_contents
- exec() file_get_contents() file_put_contents() 示例
- 【php】PHP中file_get_contents()与file_put_contents()函数细节详解
- PHP file_put_contents&file_get_contents的使用
- file_put_contents() 与 file_get_contents() 函数用法
- file_put_contents以及file_get_contents的用法与在使用过程中遇到的问题(PHP学习)
- PHP中file_put_contents追加和换行
- PHP中file_put_contents追加和换行
- PHP中file_put_contents追加和换行
- 将TXT文档中数据存入数组中 C#
- 用SQL向*.txt中追加文本
- php中file文件操作函数readfile fread fgets fgetc以及不需要加fopen的file_get_contents file_put_contents file()
- php中file文件操作函数readfile fread fgets fgetc以及不需要加fopen的file_get_contents file_put_contents file()
- 用ASP.NET向数据库中存取图片
- file_put_contents()写入数组
- file_put_contents()写入数组
- 黑客大曝光第六版
- MySQL字符集问题
- 2010年1月12日
- mschart
- SQL语法手册
- 用file_get_contents()和file_put_contents()向txt文档中存取数组
- 字符指针和字符数组区别
- ”囧“已经落伍了,你认识下面的字吗?
- StrongMouse学习ARM-Linux开发(1)
- C++文件读写
- 用PHP的DOM控件来创建XML输出
- 外连接(outer join)
- jboss集成tomcat页面压缩(gzip)的配置
- energy minimize, max flow解法