使用 array_push 存储特定行数

Store specific row count with array_push

我正在尝试存储特定数组项的计数,以便我知道要在 laravel excel

中设置哪一行的样式

我想做的是在每次执行 array_push 时增加我的迭代器 $rowCount,但每次我专门为 $groupItem 处理 array_push 时,我想将计数存储在 $boldRows 中,这样我就可以仅将样式应用于那些行。

$allgroupResult= array();

$rowCount = 2; //since I have a sticky header in the excel file I start the count at 2
$boldRows = array();

foreach($prices->groups as $group){ 
    $groupItem = array(); 
    $groupItem["category_code"] = $group->category_code;
    $groupItem["category_name"] = $group->category_name; 
    $groupItem["category_description"] = $group->category_description;

    array_push($allgroupResult, $groupItem);    

    foreach($group->skus as $sku){
        $skuItem = array(); 
        $skuItem["identifier"] = $sku->info->identifier;

        array_push($allgroupResult, $skuItem);    
    }
}

第一类有 3 种产品(总共 4 行),第二类有 2 种产品(总共 3 行),从第 2 行开始总共有 7 行。所以我的预期结果是 $boldRows 然后将包含类别行的 2 和 6(因为我的计数从 2 开始,然后处理 3 个产品,所以下一个类别行是 6)

我怎样才能正确地做到这一点?

我原以为每次将新元素推送到数组时只需增加行数并跟踪要以粗体显示的行...

$rowCount = 2; //since I have a sticky header in the excel file I start the count at 2
$boldRows = array();

foreach($prices->groups as $group){ 
    $groupItem = array(); 
    $groupItem["category_code"] = $group->category_code;
    $groupItem["category_name"] = $group->category_name; 
    $groupItem["category_description"] = $group->category_description;

    array_push($allgroupResult, $groupItem); 
    array_push($boldRows, $rowCount++);   // Store count & increment

    foreach($group->skus as $sku){
        $skuItem = array(); 
        $skuItem["identifier"] = $sku->info->identifier;

        array_push($allgroupResult, $skuItem);
        $rowCount++;    // Just increment count
    }
}

您可能需要根据行与数组的匹配方式调整 $rowCount - 数组基于 0,我不知道行将如何基于。

基于 PHPExcel Make first row bold and the row number excel row conversion from Convert A to 1 B to 2 ... Z to 26 and then AA to 27 AB to 28 (column indexes to column references in Excel)(针对 PHP 进行了修改),然后您可以使用...

foreach ( $boldRows as $row )   {
    $cell_name = excelColumnFromNumber($row)."1";
    $objPHPExcel->getActiveSheet()->getStyle( $cell_name )->getFont()->setBold( true );
}

function excelColumnFromNumber($column)
{
    $columnString = "";
    while ($column > 0)
    {
        $currentLetterNumber = ($column - 1) % 26;
        $columnString = chr($currentLetterNumber + 65) . $columnString;
        $column = ($column - ($currentLetterNumber + 1)) / 26;
    }
    return $columnString;
}