【发布时间】:2015-03-23 20:41:44
【问题描述】:
我正在学习 php 作为我学习的一部分,目前我想将一个 csv 文件读入一个数组,然后计算所有值。我已成功读取文件并可以显示 csv 文件中的所有值,但无法对它们求和/相加以求得总数。
到目前为止,这是我的代码:
<?php
$data= explode(",",
file_get_contents('https://www.mywebsite.com/test.csv')
);
$total = 0;
$lengthofarray=count($data);
for($x=0;$x<=$lengthofarray;$x++)
{
$total = $total + $x;
//I am not sure if I am missing something here in order to make it working
}
echo " ".$total."<br/>";
?>
我知道这是一个基本问题,但我花了 12 多个小时才找到解决方案,并在互联网上搜索以找到解决方案,但无法做到。
以下是我的 csv 文件中的值:
0.78
0.19
0.78
0.98
0.65
0.79
0.34
0.29
0.55
0.95
【问题讨论】: