Search code examples
phparraysphpexcelarray-push

Store specific row count with array_push


I'm trying to store a count of specific array items so that I know which row to style in laravel excel

What I would like to do is increase my iterator $rowCount any time I do an array_push, but any time I specifically process an array_push for $groupItem, I want to store the count in $boldRows that way I can apply styling to only those rows.

$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);    
    }
}

category one with 3 products (total of 4 rows) and category two with 2 products (total of 3 rows), would give me 7 total rows starting at row 2. So my expected result with this would be that $boldRows would then contain 2 and 6 for the category rows (because my count starts at 2 then processes 3 products so the next category row is at 6)

How can I properly achieve this?


Solution

  • I would have thought that you just needed to increment the rowcount each time you push a new element to the array and keep track of the rows you want to be in bold...

    $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
        }
    }
    

    You may need to adjust the $rowCount depending on how the rows match the arrays - arrays are 0 based and I don't know how the rows will be based.

    Based on 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) (modified for PHP), you can then use something like ...

    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;
    }