【发布时间】:2020-12-15 04:39:02
【问题描述】:
我正在尝试将 excel 行推送到数组中。
所以我的方式是,我使用 foreach 循环遍历 excel 文件,然后使用 for 循环 循环并为我检查值并进行一些验证。
我遇到的问题是,在我的 for 循环中,我得到了我的 excel 文件的第一行,它打印了多次,并且 我应该得到的是所有成功的行。
这是我的代码
$col = $sheet->getHighestDataColumn();
$row = $sheet->getHighestDataRow();
$rowCount = $sheet->getHighestDataRow();
$allRows = $sheet->rangeToArray("A1:{$col}{$row}", null, false);
$arr = [];
foreach($allRows as $row)
{
for($x = 0; $x <= $rowCount; $x++)
{
if($x == 0 || $x == 1) continue;
$col1 = $sheet->getCell("D$x")->getValue();
$col2 = $sheet->getCell("E$x")->getValue();
$col3 = $sheet->getCell("F$x")->getValue();
$col4 = $sheet->getCell("G$x")->getValue();
$total = $sheet->getCell("H$x")->getValue();
$testTotal = $col1 + $col2 + $col3 + $col4;
if($testTotal === $total)
{
echo "<PRE>";
echo print_r($row);
echo "</PRE>";
$arr[] = $row;
}
}
}
【问题讨论】: