doubo7131 2015-07-13 15:43
浏览 27

在PHP中连续存储数组的最后一行文件

So i have a little issue with some PHP read functionality. What I am trying to do is basically grab data into an array from a file that is being continuously updated from a python script reading values from a micro controller. So basically, the file would look something like this.

ID, Datetime, Count, Name
ID, Datetime, Count, Name
ID, Datetime, Count, Name

What i need is for it to read the new line that is being added in (eof) and store it into an array. So what i have so far is allowing read access into the file

  <?php
$myfile = fopen("read.txt", "r")

For the storing the lines in an array i figured something like an array map would be efficient

$result = array();
 // some loop
  $parts = array_map('trim', explode(':', $line_of_text, 2)));
  $result[$parts[0]] = $parts[1];

However i am not to sure on how to structure the loop to have it read the new line that is being updated in the file without exiting the loop.

while (feof($file)) {
}

fclose($file);
?>

Any help would be appreciated!!

  • 写回答

3条回答 默认 最新

  • doupapin172773 2015-07-13 15:47
    关注

    Can you do this?

    1. Read the lines of the file to an array using $lines = file("filename");.
    2. Use the $lines[count($lines) - 1] to get the last line?
    3. You can even trim off the empty lines before you wanna do this.

    Trim Empty Lines

    Use this function:

    $lines = array_filter($lines);
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP