我有一个包含许多行的文件,每一行都有以下格式: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的新手,有人可以帮助我吗?请告诉我我做错了什么?非常感谢您的时间。