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);
------解决方案--------------------
$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 $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>
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号