PHP 将文件中的数组读取到不带引号的数组中
我有一个函数将数据库查询的结果写入数组。另一个函数将该数组写入文件。
现在我想要一个可以将文件内容放回到数组中的函数。
该数组看起来像这样(当我从数据库获取它时以及它在文件中的样子):
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如有人已经说过的 - 使用某种数据格式。 CSV、JSON、XML - 任何格式都可以。
您现在所做的只是将 print_r 或 var_dump (或您使用的任何函数)的输出写入文件。这些函数的输出仅供参考,而不是用于存储数据。
以下是一些为您提供的快速功能:
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: