1

I've outputted my array to file using the print_r($myArray, true) method, but am having trouble re-importing it as an array.

I keep returning a string with the array, not the array itself. I've tried a few different combinations including print_r and serialize, but can't seem to get it right. What am I missing?

Here's what I have:

$myArray = print_r(file_get_contents($logFile), true);

for reference the log file content looks like so:

Array
(
    [0] => Array
        (
            [0] => blah
            [1] => blah
        )
...

Thanks


EDIT: Solution - Here is what I came up with:

I changed the file contents to include php tags and declared the array there using var_export instead of print_r.

Here is what I used as my content string when writing to file:

<?php $myArray = '.var_export($myArray, true).'; ?'.'>

From there it was a simple include to get the array back.

4

1 回答 1

0

您应该将数组存储为...一个数组,如下所示:

file_put_contents('myfile.txt', '<?php return ' . var_export($array, true) . '; ?>');

然后,阅读:

$array = include 'myfile.txt';

也可以看看:var_export()

于 2013-06-14T16:29:41.847 回答