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

解决方案 »

  1.   

    php 有一个函数var_dump
      

  2.   

    你把数组写入文件之前,用serialize方法序列化一下。
    想用数组时,从文件中读出内容后用unserialize() 反序列化一下
    反序列化之后得到的数组和之前你存入的数组是完全一样的
      

  3.   

    var_export这个可以直接以数组的方式写入 上面那个写错了
      

  4.   

    to 3L :具体怎么操作 ?可否实例一下??什么序列和反序列 都没听说过……
      

  5.   

    $arr=array("a","b","c","d","e");
    $arr=var_export($arr,"true");
    $fp=fopen("a.txt","w");
    fwrite($fp,$arr);
    fclose($fp);
      

  6.   


    $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
    )
    */
      

  7.   

    3楼说的方法
    <?php
    $yourArray = array('aqw568', 'sdtrnz2006', 'gggudf');// 原来的数组
    file_put_contents('aa.txt', serialize($yourArray));// 序列化并写入文件$readArray = unserialize(file_get_contents('aa.txt'));// 读取并反序列化print_r($readArray);
    ?>
      

  8.   

    test.php文件内容为:a:5:{i:0;i:1;i:1;i:55;i:2;i:2;i:3;i:36;i:4;i:77;}
      

  9.   

    写入的方法
    $textdata = '<?php
    $data=';
    $textdata .= var_export($array,true).';?>';
    //###测试临时注释
    file_put_contents('aa.php',$textdata);读出的方法
    require_once(aa.php);
    print_r($textdata);