php 在php中将数组写入文件并获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14869104/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:06:37  来源:igfitidea点击:

Writing Array to File in php And getting the data

php

提问by Angshu Guha

I have An array which looks like following after using print_r

我有一个数组,使用后如下所示 print_r

Array ( [0] => Array ( [0] => piklu [name] => piklu ) [1] => Array ( [0] => arindam [name] => arindam ) [2] => Array ( [0] => shyamal [name] => shyamal ) [3] => Array ( [0] => arko [name] => arko ) [4] => Array ( [0] => pamela [name] => pamela ) [5] => Array ( [0] => dodo [name] => dodo ) [6] => Array ( [0] => tanmoy [name] => tanmoy ) [7] => Array ( [0] => jitu [name] => jitu ) [8] => Array ( [0] => ajgar [name] => ajgar ) ) 

Now I want to write this array direct to a file, I use the file_put_contentsmethod, but I don't know how to get the data from the file exactly how they looks like original. Any idea to solve this?

现在我想将此数组直接写入文件,我使用该file_put_contents方法,但我不知道如何从文件中获取与原始数据完全相同的数据。有什么想法可以解决这个问题吗?

回答by MarcDefiant

Your problem at the moment is basically that you're only able to write strings into a file. So in order to use file_put_contentsyou first need to convert your data to a string.

您目前的问题基本上是您只能将字符串写入文件。因此,为了使用,file_put_contents您首先需要将数据转换为字符串。

For this specific use case there is a function called serializewhich converts any PHP data type into a string (except resources).

对于这个特定用例,有一个名为serialize的函数,它可以将任何 PHP 数据类型转换为字符串(资源除外)。

Here an example how to use this.

这是一个如何使用它的示例。

$string_data = serialize($array);
file_put_contents("your-file.txt", $string_data);

You probably also want to extract your data later on. Simply use unserializeto convert the string data from the file back to an array.

您可能还想稍后提取您的数据。只需使用反序列化将文件中的字符串数据转换回数组。

This is how you do it:

这是你如何做到的:

$string_data = file_get_contents("your-file.txt");
$array = unserialize($string_data);

回答by Patrick Moore

Here are two ways:

这里有两种方法:

(1) Write a JSON representation of the array object to the file.

(1) 将数组对象的 JSON 表示写入文件。

$arr = array( [...] );
file_put_contents( 'data.txt', json_encode( $arr ) );

Then later...

然后后来...

$data = file_get_contents( 'data.txt' );
$arr = json_decode( $data );

(2) Write a serialized representation of the array object to the file.

(2) 将数组对象的序列化表示写入文件。

$arr = array( [...] );
file_put_contents( 'data.txt', serialize( $arr ) );

Then later...

然后后来...

$data = file_get_contents( 'data.txt' );
$arr = unserialize( $data );

I prefer JSON method, because it doesn't corrupt as easily as serialize. You can open up the data file and make edits to the contents, and it will encode/decode back without big headaches. Serialized data cannot be changed or corrupted, or unserialize() won't work.

我更喜欢 JSON 方法,因为它不像序列化那样容易损坏。您可以打开数据文件并对其内容进行编辑,它会编码/解码而不会让人头疼。序列化数据不能更改或损坏,否则 unserialize() 将不起作用。

回答by Sites Done Right

file_put_contentswrites a string to a file, not an array. http://php.net/manual/en/function.file-put-contents.php

file_put_contents将字符串写入文件,而不是数组。http://php.net/manual/en/function.file-put-contents.php

If you'd like to write what you see there in that print_r to a file, you can try this:

如果您想将在 print_r 中看到的内容写入文件,可以尝试以下操作:

ob_start();
print_r($myarray);
$output = ob_get_clean();
file_put_contents("myfile.txt",$output);

回答by Devang Rathod

i m not sure but maybe its something like this. You want to serialize() the array on writting. it will put your array into test.txt

我不确定,但也许是这样的。您想在写入时序列化()数组。它会将您的数组放入 test.txt

file_put_contents('test.txt', serialize($array));