Home >Backend Development >PHP Tutorial >php 求教怎么将一数组写入文件aa.txt,而后怎样读取aa.txt文件到一数组

php 求教怎么将一数组写入文件aa.txt,而后怎样读取aa.txt文件到一数组

WBOY
WBOYOriginal
2016-06-13 10:10:011098browse

php 求教如何将一数组写入文件aa.txt,而后怎样读取aa.txt文件到一数组?
php 求教如何将一数组array写入文件aa.txt,而后怎样读取aa.txt文件到一数组?
Arry数组的内容如下:
Array ( [0] => aqw568 [1] => sdtrnz2006 [2] => gggudf );
可以直接读取数组:print_r($str = file_get_contents ("aa.txt"))吗??;

------解决方案--------------------
应该不可以吧.
------解决方案--------------------
php 有一个函数var_dump

------解决方案--------------------
你把数组写入文件之前,用serialize方法序列化一下。
想用数组时,从文件中读出内容后用unserialize() 反序列化一下
反序列化之后得到的数组和之前你存入的数组是完全一样的
------解决方案--------------------
var_export这个可以直接以数组的方式写入 上面那个写错了
------解决方案--------------------
$arr=array("a","b","c","d","e");
$arr=var_export($arr,"true");
$fp=fopen("a.txt","w");
fwrite($fp,$arr);
fclose($fp);

------解决方案--------------------

PHP code
$arr_old = array(1,55,2,36,77);print_r($arr_old);/*Array(    [0] => 1    [1] => 55    [2] => 2    [3] => 36    [4] => 77)*/$file_name_old="test.php";$file_old=fopen($file_name_old,"w");fwrite($file_old,serialize($arr_old));fclose($file_old);$file_name_new="test.php";$file_new=fopen($file_name_new,"r");$file_read = fread($file_new, filesize($file_name_new)); fclose($file_new);$arr_new = unserialize($file_read);print_r($arr_new);/*Array(    [0] => 1    [1] => 55    [2] => 2    [3] => 36    [4] => 77)*/<br><font color="#e78608">------解决方案--------------------</font><br>3楼说的方法<br>
PHP code
<?php $yourArray = array('aqw568', 'sdtrnz2006', 'gggudf');// 原来的数组file_put_contents('aa.txt', serialize($yourArray));// 序列化并写入文件$readArray = unserialize(file_get_contents('aa.txt'));// 读取并反序列化print_r($readArray);?><div class="clear">
                 
              
              
        
            </div>
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