3 回答

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超7個(gè)贊
它是一個(gè) csv 文件,您可以用,除第一個(gè)標(biāo)題行之外的最后一項(xiàng)來(lái)分解每一行并求和。
$lines = file($file_path);
$lines = array_map(function($v){return explode(",",$v);},array_slice($lines,1));
echo array_sum(array_column($lines,2)) . PHP_EOL;

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊
嘗試使用preg_match_all和 RegEx
function readAFile()
{
$userfileInfo = fopen("peopleInformation.txt", "r") or die("Unable to open the file.");
//echo fread($userfileInfo, filesize("peopleInformation.txt"));
$theData = fread($userfileInfo, filesize("peopleInformation.txt"));
echo $theData;
preg_match_all('/\d+,.+,(\d+)/', $theData, $output);
$sum = 0;
foreach($output[1] as $value){
$sum = $sum + (int) $value;
};
//echo "Sum :".$sum;
fclose($userfileInfo);
}

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
您可以使用fgets函數(shù)逐行讀取它。然后您可以輕松地使用explode函數(shù)將行更改為數(shù)組。數(shù)組中的最后一個(gè)索引是您想要的值。如下所示:
$theData = fopen("peopleInformation.txt", "rw");
fgets($theData); //skip the header.
$sum = 0;
while (! feof ($my_file))
{
$line = fgets($theData);
$array_val = explode($line);
$sum += (int)end($array_val);
}
- 3 回答
- 0 關(guān)注
- 112 瀏覽
添加回答
舉報(bào)