【问题标题】:For loop displaying the same rowFor循环显示同一行
【发布时间】: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;
            }
        }
    }

【问题讨论】:

    标签: php laravel laravel-8


    【解决方案1】:

    您将多次打印第一行,因为您在 for 循环中打印它,而该循环本身位于 foreach 循环中。

    您不需要在 foreach 循环中进行验证,因为验证不使用 $row。

    在外面进行验证,然后继续使用 $allRows

    $col = $sheet->getHighestDataColumn();
    $row = $sheet->getHighestDataRow();
    $rowCount = $sheet->getHighestDataRow();
    $allRows = $sheet->rangeToArray("A1:{$col}{$row}", null, false);
    
    $validatedRowCount = 0;
    
    //Do the validation
    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;
    
        $validatedRowCount = $testTotal === $total 
            ? $validatedRowCount + 1 
            : $validatedRowCount;            
    }
    
    if($validatedRowCount === $rowCount) {
    //Safe to assume that $allRows contains validated data
    //Proceed to use $allRows
    }
    

    或者,如果您想验证每一行并通过验证过滤每一行,那么

    
    $col = $sheet->getHighestDataColumn();
    $row = $sheet->getHighestDataRow();
    $rowCount = $sheet->getHighestDataRow();
    $allRows = $sheet->rangeToArray("A1:{$col}{$row}", null, false);
    
    $validatedRows = [];
    
    foreach($allRows as $index => $row)
    {
        $col1 = $sheet->getCell("D$index")->getValue();
        $col2 = $sheet->getCell("E$index")->getValue();
        $col3 = $sheet->getCell("F$index")->getValue();
        $col4 = $sheet->getCell("G$index")->getValue();
        $total = $sheet->getCell("H$index")->getValue();
    
        $testTotal = $col1 + $col2 + $col3 + $col4;
    
        if($testTotal === $total) {
             echo "<PRE>";
             echo print_r($row);
             echo "</PRE>";
            $validatedRows[] = $row;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多