PHP 将文件中的数组读取到不带引号的数组中

发布于 2024-12-17 16:03:03 字数 1105 浏览 0 评论 0原文

我有一个函数将数据库查询的结果写入数组。另一个函数将该数组写入文件。

现在我想要一个可以将文件内容放回到数组中的函数。

该数组看起来像这样(当我从数据库获取它时以及它在文件中的样子):

Array
(
[0] => Array
    (
        [0] => 0605
        [TIME] => 0605
        [1] => AGP
        [CODE] => AGP
    )

[1] => Array
    (
        [0] => 0610
        [TIME] => 0610
        [1] => NBE
        [CODE] => NBE
    )

[2] => Array
    (
        [0] => 0610
        [TIME] => 0610
        [1] => AYT
        [CODE] => AYT
    )

[3] => Array
    (
        [0] => 0620
        [TIME] => 0620
        [1] => TFS
        [CODE] => TFS
    )
)

我已经尝试过这个 - >

public function file2Array($file){

    $filename = fopen($file,"r"); 
    $arr = array(); 

        while(!feof($filename)) 
        { 
    //read file line by line into a new array element 
    $arr[] = fgets($filename, 4096); 
    } 
    fclose ($filename); 
    return $arr;
    }

它有效,但我的数组在每行前面都有一个(单)引号返回(当然使数组无用)。

我如何将文件中的数组返回到不带引号的数组中,就像文件中一样?

编辑:也许我应该提到正在读取的文件具有 .php 扩展名

I have a function that writes a result from a db query into an array. Another function writes this array into a file.

Now I want a function that can put the contents of the file back into an array.

The array looks like this(both when i get it from the db and how it looks in the file):

Array
(
[0] => Array
    (
        [0] => 0605
        [TIME] => 0605
        [1] => AGP
        [CODE] => AGP
    )

[1] => Array
    (
        [0] => 0610
        [TIME] => 0610
        [1] => NBE
        [CODE] => NBE
    )

[2] => Array
    (
        [0] => 0610
        [TIME] => 0610
        [1] => AYT
        [CODE] => AYT
    )

[3] => Array
    (
        [0] => 0620
        [TIME] => 0620
        [1] => TFS
        [CODE] => TFS
    )
)

i have tried this ->

public function file2Array($file){

    $filename = fopen($file,"r"); 
    $arr = array(); 

        while(!feof($filename)) 
        { 
    //read file line by line into a new array element 
    $arr[] = fgets($filename, 4096); 
    } 
    fclose ($filename); 
    return $arr;
    }

And it works but my array is returned with a (single)quote in front of every line(rendering the array useless ofcourse).

How would i return the array from the file into an array without quotes, exactly like in the file?

edit: Maybe i should mention that the file being read has .php extension

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳哥哥 2024-12-24 16:03:03

正如有人已经说过的 - 使用某种数据格式。 CSV、JSON、XML - 任何格式都可以。

您现在所做的只是将 print_r 或 var_dump (或您使用的任何函数)的输出写入文件。这些函数的输出仅供参考,而不是用于存储数据。

以下是一些为您提供的快速功能:

function array_to_file(array $array, $filename)
{
 $handle = fopen($filename, 'w');
 foreach ($array as $a) {
     $string = json_encode($a).PHP_EOL;
     fwrite($handle, $string);
   } 
   fclose($handle)
}

function file_to_array($filename)
{
   $result = array();
   $array_of_lines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
   foreach ($array_of_lines as $line){
     $result[] = json_decode($line, true);
   }
   return $result;
}

As someone already said - use some data format. CSV, JSON, XML - anything will do.

What you are doing now is just writing output of print_r or var_dump (or whatever function you use) to a file. Output of those functions are for informational purposes, NOT for storing data.

Here are some quick functions for you:

function array_to_file(array $array, $filename)
{
 $handle = fopen($filename, 'w');
 foreach ($array as $a) {
     $string = json_encode($a).PHP_EOL;
     fwrite($handle, $string);
   } 
   fclose($handle)
}

function file_to_array($filename)
{
   $result = array();
   $array_of_lines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
   foreach ($array_of_lines as $line){
     $result[] = json_decode($line, true);
   }
   return $result;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文