0
我正在阅读Excel中的Excel文件。我想在最后一行显示一列的总数。要做到这一点,我需要得到下面数组的总和。在PHP中,从Excel文件生成数组的总和数组
的代码以获得下面阵列的一部分,
阵列([0] => 1)阵列([0] => 2)
$file = "$filename";
$sheet = (isset($_POST['sheet'])) ? $_POST['sheet'] : '';
$connection = new Spreadsheet_Excel_Reader();
$connection->read($file);
echo"<table>";
$x = 1;
while ($x <= $connection->sheets[$sheet]['numRows']) {
echo "\t<tr>\n";
$y=1;
while ($y <= $connection->sheets[$sheet]['numCols']) {
$cell = isset($connection->sheets[$sheet]['cells'][$x][$y]) ? $connection->sheets[$sheet]['cells'][$x][$y] : '';
echo "\t\t<td>$cell</td>\n";
$y++;
}
echo "\t</tr>\n";
$x++;
}
$x = 2;
while ($x <= $connection->sheets[$sheet]['numRows']) {
$ctnqty = isset($connection->sheets[$sheet]['cells'][$x][16]) ?
$connection->sheets[$sheet]['cells'][$x][16] : '';
$ctnqtyttl = explode(",",$ctnqty);
print_r ($ctnqtyttl);
$x++;
}
我起诉下面的代码来得到上面数组的总和,但它给我的结果只有2。请帮我在这里得到的总和3.
$sum = 0;
foreach ($ctnqtyttl as $value) {
echo $sum = $sum + $value;
}
它确实有助于阅读和理解的代码(甚至你自己)当你有它正确地缩进。 – trincot