dongzi1959 2017-03-11 08:07
浏览 110
已采纳

PHP读取文件并获取最大值和最小值

my non-programmer-brain reached the limit. I'm collecting the temperature-value of a sensor in a text file (raspberry pi with a bash-script), easy stuff:

23.2
24.5
12.8
6.8
27.9

and so on. I now try to read the file (PHP) to echo the highest and lowest temperatur in the list. I've found a simple solution in the www, which is based on this input/start:

$temperatures = array(78,60,62,1,68,73);

echo "5 lowest temperatures: ";
echo implode(", ", array_slice($temperatures, 0, 5));

echo "Average temperature: ";
echo number_format(array_sum($temperatures) / count($temperatures), 1);
..(and so on)..

What I now try to accomplish, is to read the file and insert the values into this array for $temperatures. But I don't understand the way from read in the file line by line via explode and then implode with , so that I can use it for the output. I don't want to assign other values, there are no headers, no multi-arrays and whatever... I guess the solution is damn easy, but I just see only more arrays and millions of solutions to assign, combine, split and other stuff. Maybe it's too easy for you and to complicate for my world. Any help would be nice - also, if you have a complete other or better idea for the target/result? Thanks in advance & sorry for my bad english!

  • 写回答

1条回答 默认 最新

  • donglang8008 2017-03-11 08:13
    关注

    You can use simple function to get array of values from file:

    $temperatures = file($file, FILE_IGNORE_NEW_LINES)
    

    To get min max values use functions:

    $min = min($temperatures);
    $max = max($temperatures);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊