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

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 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。