从php中的文件读取保存的文本

时间:2013-08-03 16:47:20

标签: php file reference

我创建了一个生成数组的脚本。 需要存储该数组,以便如果用户想要导出其内容(例如打印),则可能是这样     这是我的数组($ doc)

Array  
     (  
    [0] => Array  
    (  
        [0] => ------  
        [1] => ---Start----  
        [2] => -------  
        [3] => Generated on Saturday, 3rd, August 2013  
    )

[1] => Array
    (
        [0] => ------
        [1] => -------
        [2] => -------
        [3] => -----------------------
    )

[2] => Array
    (
        [0] => Tid
        [1] => Policy
        [2] => Budget
        [3] => User
        [4] => Capturetime
        [5] => Country
    )

[3] => Array
    (
        [0] => 1
        [1] => ask
        [2] => das
        [3] => carol
        [4] => 2013-07-09
        [5] => Ethiopia
    )

[4] => Array
    (
        [0] => 2
        [1] => das
        [2] => adsasd
        [3] => ck
        [4] => 2007-07-13
        [5] => Slovakia
    )

[5] => Array
    (
        [0] =>  
        [1] =>  
        [2] =>  
        [3] =>  
    )

[6] => Array
    (
        [0] => ------
        [1] => ---End----
        [2] => -------
    )

)

现在我将数组保存到文件中:
file_put_contents('array.txt', print_r($printdoc, true));
然后我想回调array.txt的内容到数组$ printdoc并将其用作数组。 $printdoc = file_get_contents('array.txt', true); 我得到$ printdoc作为字符串而不是数组
有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

您可以使用serialize()将数组转换为可序列化的字符串,然后将其存储在文件中。当您从文件中读取字符串时,请使用unserialze()将其转换回数组。