Home  >  Article  >  php教程  >  php中将一段数据存到一个txt文件中并显示其内容,

php中将一段数据存到一个txt文件中并显示其内容,

WBOY
WBOYOriginal
2016-06-13 09:27:11826browse

php中将一段数据存到一个txt文件中并显示其内容,

这里的数据可以为基本数据类型,数组,对象等;

在存储的时候可以用serialize进行序列化,但取的时候要先用unserialize反序列化。

<&#63;php
  $data = array("上海","西安","北京");

  //将数组存到指定的text文件中
  file_put_contents("E:/data.txt",serialize($data));
  //获取数据
  $datas = unserialize(file_get_contents("E:/data.txt"));
  print_r($datas);
&#63;>

当然也可以使用json_encode,这里数组可以以键值对存取,取时要用json_decode转义。

<&#63;php
  $data = array("现代"=>"上海","文化"=>"西安","首都"=>"北京");

  //将数组存到指定的text文件中
  file_put_contents("E:/data.txt",json_encode($data));
  //获取数据
  $datas = json_decode(file_get_contents("E:/data.txt"));
  print_r($datas);
&#63;>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn