php PHP数组保存到文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13432632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Array saved to Text file
提问by coffeemonitor
I've saved a response from an outside server to a text file, so I don't need to keep running connection requests. Instead, perhaps I can use the text file for my manipulation purposes, until I'm read for re-connecting again. (also, my connection requests are limited to this outside server)
我已将来自外部服务器的响应保存到文本文件,因此我不需要继续运行连接请求。相反,也许我可以将文本文件用于我的操作目的,直到我再次阅读以重新连接。(另外,我的连接请求仅限于这个外部服务器)
Here is what I've saved to a text file:
这是我保存到文本文件中的内容:
records.txt
记录.txt
Array
(
[0] => stdClass Object
(
[id] => 552
[date_created] => 2012-02-23 10:30:56
[date_modified] => 2012-03-09 18:55:26
[date_deleted] => 2012-03-09 18:55:26
[first_name] => Test
[middle_name] =>
[last_name] => Test
[home_phone] => (123) 123-1234
[email] => [email protected]
)
[1] => stdClass Object
(
[id] => 553
[date_created] => 2012-02-23 10:30:56
[date_modified] => 2012-03-09 18:55:26
[date_deleted] => 2012-03-09 18:55:26
[first_name] => Test
[middle_name] =>
[last_name] => Test
[home_phone] => (325) 558-1234
[email] => [email protected]
)
)
There's actually more in the Array, but I'm sure 2 are fine.
Array 中实际上还有更多,但我确定 2 没问题。
Since this is a text file, and I want to pretend this is the actual outside server (sending me the same info), how do I make it a real array again?
由于这是一个文本文件,并且我想假装这是实际的外部服务器(向我发送相同的信息),我如何再次使其成为真正的数组?
I know I need to open the file first:
我知道我需要先打开文件:
<?php
$fp = fopen('records.txt', "r"); // open the file
$theData = fread($fh, filesize('records.txt'));
fclose($fh);
echo $theData;
?>
So far $theDatais a string value. Is there a way to convert it back to the Array it originally came in as?
到目前为止$theData是一个字符串值。有没有办法将它转换回它最初作为的数组?
回答by rajukoyilandy
Better serialize and save to file, then unserialize back to array.
更好地序列化并保存到文件,然后反序列化回数组。
// serialize your input array (say $array)
$serializedData = serialize($array);
// save serialized data in a text file
file_put_contents('your_file_name.txt', $serializedData);
// at a later point, you can convert it back to array like:
$recoveredData = file_get_contents('your_file_name.txt');
// unserializing to get actual array
$recoveredArray = unserialize($recoveredData);
// you can print your array like
print_r($recoveredArray);
回答by mario
You shouldn't have saved it in print_rformat then.
print_r那时你不应该以格式保存它。
Use either:
使用:
That makes it simple to decode the file back into an array.
这使得将文件解码回数组变得简单。
Albeit there is a print_r decoder. But that should be the last resort, only if you cannot affect the input data (which you can!).
尽管有一个print_r 解码器。但这应该是最后的手段,前提是您不能影响输入数据(您可以!)。
回答by NullPoiиteя
You could serializethe array before writing it as text to a file. Then you can read the data back out of the file unserializewill turn it back into an array.
您可以serialize先将数组作为文本写入文件。然后您可以从文件中读取数据,unserialize将其重新转换为数组。
回答by Cristian Cepeda
JSON Version
JSON 版本
$json_data = json_encode($the_array);
file_put_contents("records.txt", $json_data);
// Recovering
$the_data = file_get_contents("records.txt");
$the_array = json_decode($the_data);

