php 文件将内容放入数组

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

file put contents with array

php

提问by Shishant

I am using file_put_contents($file, $data);function for putting contents in file, because these files are created on fly with name of sessions, $datais an multi-dimensional array, when i am echoing array it prints out fine but in file no contents get recorded except the word Array

我正在使用file_put_contents($file, $data);函数将内容放入文件中,因为这些文件是使用会话名称动态创建的,$data是一个多维数组,当我回显数组时,它打印出来很好,但在文件中除了单词之外没有记录任何内容大批

What should i do or is there any other function which automatically creates the file and records the data (array)?

我应该怎么做或者是否有任何其他功能可以自动创建文件并记录数据(数组)?

Thank You.

谢谢你。

回答by Philippe Gerber

You want to serialize()the array on writting, and unserialize()after reading the file.

你想在写入serialize()数组时,并unserialize()在读取文件后。

$array = array('foo' => 'bar');
file_put_contents('foo.txt', serialize($array));
$array = unserialize(file_get_contents('foo.txt')));

Oh, and I really don't now how you echo'd your array, but echo array('foo' => 'bar');will always print Array.

哦,我现在真的不知道你如何回应你的数组,但echo array('foo' => 'bar');总是会打印Array.

回答by Shishant

If you want it to be readable, you could do:

如果你想让它可读,你可以这样做:

<?php
ob_start();
print_r($data);
$textualRepresentation = ob_get_contents();
ob_end_clean();

file_put_contents($file, $textualRepresentation);
?>

回答by Robert Sinclair

Or use print_rif you want to return a formatted array that is more easily readable in the txt file

或者,如果您想返回一个在 txt 文件中更易于阅读的格式化数组,请使用print_r

If you would like to capture the output of print_r(), use the return parameter. When this parameter is set to TRUE, print_r() will return the information rather than print it.

如果您想捕获 print_r() 的输出,请使用返回参数。当此参数设置为 TRUE 时,print_r() 将返回信息而不是打印它。

Code is:

代码是:

$txt = "<pre>".print_r($data, true)."</pre>";
file_put_contents('file.txt', $txt);

回答by inakiabt

Definetelly you can use var_exportfunction:

定义你可以使用var_export函数:

$contents = var_export($array, true);

file_put_contents('foo.txt', "<?php\n return {$contents};\n ?>");
// and then use something like this to get it:
$new_array = require('foo.txt');

回答by typeoneerror

You could serializethe array and then userialize it after you load the file back in. You could also encode the array as a JSON object with json_encodeand write to a .json file.

您可以序列化数组,然后在重新加载文件后对其进行用户化。您还可以使用json_encode将数组编码为 JSON 对象并写入 .json 文件。

回答by MJanaTiI

A better solution if you are familiar with var_dump()output could be to capture it's output and put it into the file, or even print_r()as follow:

如果您熟悉var_dump()输出,更好的解决方案可能是捕获它的输出并将其放入文件中,甚至print_r()如下所示:

ob_start();
print_r($array);
$result = ob_get_clean();
file_put_contents('file.txt', $result);

note the ob_get_clean()essentially executes both ob_get_contents()and ob_end_clean().

注意ob_get_clean()本质上同时执行ob_get_contents()ob_end_clean()

回答by alxp

You can set up a recursive function that can traverse each member of an array, check if it is itself an array, and if so call the function again on this member, and if not just print out its contents. Like this:

您可以设置一个递归函数,它可以遍历数组的每个成员,检查它本身是否是一个数组,如果是,则对该成员再次调用该函数,如果不是,则仅打印出其内容。像这样:

function file_put_contents_deep( $file, $data) {
  if ( is_array( $data ) {
    foreach ( $data as $item ) {
      if (is_array( $item ) {
        file_put_contents_deep( $file, $item );
      else {
        file_put_contents( $file, $item);
      }
    }
  } else {
    file_put_contents( $file, $data );
  }
}

回答by Dan Bray

As the best answer states, using serializeis a good option. However, you can also store the array by using PHP_EOLafter each item of the array and use the filecommand to retrieve the array.

正如最佳答案所述,使用serialize是一个不错的选择。但是,您也可以通过使用PHP_EOLafter数组的每一项来存储数组,并使用该file命令来检索数组。

function file_put_array($path, $contents)
{
    if (is_array($contents))
    {
        foreach ($contents as $item)
            $str .= $item.PHP_EOL;
        file_put_contents($path, $str);
        return true;
    }
    return false;
}

And to retreive the contents:

并检索内容:

$contents = file($path, FILE_IGNORE_NEW_LINES);

回答by Vinod saraswat

$content="< ?php"."\r\n";
$str = json_encode($array);
$content.="$";
$content.="list=json_decode('";
$content.=$str;
$content.="');";
$ds=DIRECTORY_SEPARATOR;
file_put_contents(__DIR__ . $ds . 'list.php', $content);