将PHP文件解析为数组的数组

3
我有一个包含许多行的文件,每一行都有以下格式:1519382994.85#MSG#Something went wrong 因此,每一行都有三个由#分隔的字段。一个数字,一个消息类型和一个字符串。
现在我想要读取该文件并拆分内容。 我是这样做的:
//Opening the logger file
$myfile = file_get_contents("operations.txt", "r") or die("Unable to open file!");
$rows = explode("\n", $myfile);
$num_rows = count($rows);
$fieldList = array();
//Parsing rows using '#'
foreach ($rows as $row => $data) {
    $row_data = explode('#', $data);
    array_push($fieldList, (string)$row_data[0]);
    array_push($fieldList, (string)$row_data[1]);
    array_push($fieldList, (string)$row_data[2]);
}

代码运行良好,但我想要一个数组的数组和这种数据:
0: Array [ "112323.76", "MSG", "Hello"]
1: Array [ "453435.78", "MSG", "Bye"] etc..

我用了这段代码,但是似乎做错了什么。

  $last=0;
 $result = array();
        for ($i = 0; $i < $num_rows; $i++) {
        array_push($result, (string) $fieldList[$last], (string) $fieldList[$last+1],(string) $fieldList[$last+2]);
        //echo $fieldList[$last].'<br>';
        //echo $fieldList[$last+1].'<br>';
        //echo $fieldList[$last+2].'<br>';
        $last=$last+3;
        }

我是PHP的新手,有人可以帮助我吗?请告诉我我做错了什么?非常感谢您的时间。

2个回答

3

您可以利用内置的fgetcsv函数:

array fgetcsv ( resource $handle [, int $length = 0 [, string $delimiter = "," [, string $enclosure = '"' [, string $escape = "\\" ]]]] )

这可能看起来像这样:

$rows = [];
if (false !== ($handle = fopen("path/to/file", "r"))) 
{
    while (false !== ($row = fgetcsv($handle, 1000, ",")))
    {
        array_push($rows, $row);
    }
    fclose($handle);
}

我不确定这种方法是否更快,但对我来说看起来更容易。相比于 file()explode(),这种方法的主要好处有:
  1. 无需一次性将整个文件加载到内存中,可以逐行处理。
  2. 易于支持其他“字符分隔值”类型的文件,其中字段可能带有引号($enclosure)。

1

我只需要对您的代码进行一些修改。添加了对修改行的注释-

$myfile = file_get_contents("operations.txt", "r") or die("Unable to open file!");
$rows = explode("\n", $myfile);
$num_rows = count($rows);
$finalFieldList = array(); // new array
//Parsing rows using '#'
foreach ($rows as $row => $data) {
    $fieldList = array(); // temporary array
    $row_data = explode('#', $data);
    array_push($fieldList, (string)$row_data[0]);
    array_push($fieldList, (string)$row_data[1]);
    array_push($fieldList, (string)$row_data[2]);

    array_push($finalFieldList, $fieldList); // it will push to final array containing all 3 values
}

这个程序在某种程度上是工作的,但我遇到了这个错误。第一次迭代是正确的。{"0":{"0":"1519382994.96","1":"MSG","2":"blablawentok"} 第二次是错误的 "1":{"0":"1519382994.96","1":"MSG","2":"blablawenok","3":"1519382994.96","4":"MSG","5":"Processato CSV"}。所以每次迭代都有一个增量错误。有什么提示吗? :) - Nic
你在那段代码中犯了同样的错误。你可以使用另一个临时数组来获得正确的格式,或者最好在第一个foreach循环中完成。 - Jigar Shah
以这种方式解决了,但我认为并不优雅... foreach ($rows as $row => $data) { $row_data = explode('#', $data); array_push($fieldList, (string)$row_data[0]); array_push($fieldList, (string)$row_data[1]); array_push($fieldList, (string)$row_data[2]); array_push($result, $fieldList); // 它将推到包含所有3个值的最终数组中 unset($foo); // 打破引用 $fieldList = array(); // 重新初始化为空数组} - Nic
是的,它不是。你可以查看上面的答案以获取得到所需输出的好方法。 :) - Jigar Shah
1
我忘记添加临时数组了。现在它像魔法一样运作。谢谢Alive。 - Nic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接