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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测