简体   繁体   中英

Read saved text from a file an array in php

I have created a script that generates an array. This array needs to be stored so that if a user wants to export its contents( eg print ) it may be possible.
This is my array ($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] => -------
    )

)

Now I save the array to a file:
file_put_contents('array.txt', print_r($printdoc, true));
Then I would like to call back the contents of array.txt to the array $printdoc and use it as an array. $printdoc = file_get_contents('array.txt', true); I get $printdoc as a string instead of an array
Any help?

You can use serialize() to convert your array into a serializable string, then store it in a file. When you read the string from the file use unserialze() to convert it back into an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM