读取保存在文件中的 php 数组?

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

reading a php array saved in a file?

phpfile-io

提问by shaheer

i have some php arrays saved in a .log file

我有一些 php 数组保存在一个 .log 文件中

i want to read them into a php array such as

我想将它们读入一个 php 数组,例如

array[0] = 1st array in the .log file array1= 2nd array in the .log file

array[0] = .log 文件中的第一个数组 array 1= .log 文件中的第二个数组

the solution heredoes not work for me

这里的解决方案对我不起作用

it gives no such file or directory error but when i do include_once('file.log') the content in the file is displayed as the output ( i dont know why ) please help

它没有给出这样的文件或目录错误,但是当我执行 include_once('file.log') 时,文件中的内容显示为输出(我不知道为什么)请帮助

回答by Chris Baker

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将其重新转换为数组。

http://php.net/manual/en/function.serialize.php

http://php.net/manual/en/function.serialize.php

EDITDescribing the process of using serialize/unserialize:

编辑描述使用序列化/反序列化的过程:

So you have an array:

所以你有一个数组:

$arr = array(
  'one'=>array(
      'subdata1',
      'subdata2'
  ),
  'two'='12345'
);

When I call serializeon that array, I get a string:

当我调用serialize该数组时,我得到一个字符串:

$string = serialize($arr);
echo $string;

OUTPUT: a:2:{s:3:"one";a:2:{i:0;s:8:"subdata1";i:1;s:8:"subdata2";}s:3:"two";s:5:"12345";}

So I want to write that data into a file:

所以我想将该数据写入文件:

$fn= "serialtest.txt";
$fh = fopen($fn, 'w');
fwrite($fh, $string);
fclose($fh);

Later, I want to use that array. So, I'll read the file, then unserialize:

后来,我想使用那个数组。所以,我将读取文件,然后反序列化:

$str = file_get_contents('serialtest.txt');
$arr = unserialize($str);
print_r($arr);

OUTPUT: Array ( [one] => Array ( [0] => subdata1 [1] => subdata2 ) [two] => 12345 ) 

Hope that helps!

希望有帮助!

EDIT 2Nesting demo

编辑 2嵌套演示

To store more arrays into this file over time, you have to create a parent array. This array is a container for all the data, so when you want to add another array, you have to unpack the parent, and add your new data to that, then repack the whole thing.

随着时间的推移,要在此文件中存储更多数组,您必须创建一个父数组。这个数组是所有数据的容器,所以当你想添加另一个数组时,你必须解压父数组,并将新数据添加到其中,然后重新打包整个内容。

First, get your container set up:

首先,设置您的容器:

// Do this the first time, just to create the parent container
$parent = array();
$string = serialize($arr);
$fn= "logdata.log";
$fh = fopen($fn, 'w');
fwrite($fh, $string);
fclose($fh);

Now, from there forward, when you want to add a new array, first you have to get the whole package out and unserialize it:

现在,从那里开始,当你想添加一个新数组时,首先你必须取出整个包并反序列化它:

// get out the parent container
$parent = unserialize(file_get_contents('logdata.log'));

// now add your new data
$parent[] = array(
  'this'=>'is',
  'a'=>'new',
  'array'=>'for',
  'the'=>'log'
);

// now pack it up again
$fh = fopen('logdata.log', 'w');
fwrite($fh, serialize($parent));
fclose($fh);

回答by JG Estiot

It really takes 2 lines of code ($ar is the array and $filename is the path+filename to where you want to save):

它确实需要 2 行代码($ar 是数组,$filename 是要保存的路径+文件名):

To save the array:

要保存数组:

file_put_contents($filename, serialize($ar));

file_put_contents($filename, 序列化($ar));

to read the array back:

读回数组:

$ar=unserialize(file_get_contents($filename));

$ar=unserialize(file_get_contents($filename));

回答by Highly Irregular

include_once displays output, so that's not unexpected. Can you please provide the file format?

include_once 显示输出,所以这并不意外。你能提供文件格式吗?

You can use file_get_contents('file.log') to get the contents of the file, then easily put it into an array from there (eg using preg_split).

您可以使用 file_get_contents('file.log') 获取文件的内容,然后轻松地将其放入数组中(例如使用 preg_split)。

回答by Mr Coder

you need to "return" the array , open the log file.log and at the top add return

您需要“返回”数组,打开日志 file.log 并在顶部添加返回