Counting rows by array_push

Viewed 363

I'm a bit stuck here

I have an array that I'm exporting with laravel excel and I'm basically creating a category row and then all subsequent item rows that belong to that category.

How can I properly add a counter for every array_push to the groupItem array so that I can count every row between the groupItem push and set the row with the category info to bold?

Basically I only want to bold the row that has the data for category_code, category_name and category_desc so I would need to iterate based on the array_push for the category info I believe

I think I would need to set a count, increase count for the categoryItem array_push, store that count in an array and then set those array stored rows to bold?

$allgroupResult= array();

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

        array_push($allgroupResult, $groupItem);    

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

            foreach($sku->prices as $price => $amount){
                $skuItem[] = $amount;
            }

            $skuItem[] = strip_tags(html_entity_decode($sku->sku_info->item->desc));

            foreach ($sku->sku_info->details as $details) {

                $skuItem[] = $details->details1;
                $skuItem[] = $details->details2;
                $skuItem[] = $details->details3;

            }

            array_push($allgroupResult, $skuItem);    
        }
    }

    $name = 'File Export';

    $build = Excel::create($name, function ($excel) use ($allgroupResult) {

        $excel->setTitle('File Export');

        $excel->sheet('File  Export', function ($sheet) use ($allgroupResult) {

            $sheet->fromArray($allgroupResult);

            // bold the column headers
            $sheet->getStyle('A1:'.$sheet->getHighestColumn().'1')->getFont()->setBold(true);


            // $count = 2;
            // foreach($excelRows as $one){
            //     $sheet->fromArray($one, null, 'A2');

            //     $sheet->row($count, function($row) {
            //         $row->setFontWeight('bold');
            //     });
            //     $count += count( $one ) + 1;
            // }

            // set the width for the columns that are used 
            $sheet->setWidth('A', 10);
            $sheet->setWidth('B', 24);
            $sheet->setWidth('C', 20);
            $sheet->setWidth('D', 12);
            $sheet->setWidth('E', 10);
            $sheet->setWidth('F', 16);
            $sheet->setWidth('G', 16);
            $sheet->setWidth('H', 16);
            $sheet->setWidth('I', 16);
            $sheet->setWidth('J', 16);
            $sheet->setWidth('K', 16);

        });

    })->download('xlsx');
1 Answers

Why not creating another folded array inside $allgroupResult for each category, so having structure like this:

array(1) {
  [0] =>
  array(4) {
    'category_code' =>
    string(13) "category_code"
    'category_name' =>
    string(13) "category_name"
    'category_desc' =>
    string(13) "category_desc"
    'skus' =>
    array(3) {
      [0] =>
      string(4) "sku1"
      [1] =>
      string(4) "sku2"
      [2] =>
      string(4) "sku3"
    }
  }
}

and then you can just do count($item['skus']) whenever you need to get the number of products in every category. In order to do this , try the following modification to your foreach loop:

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

    $groupItem["skus"] = array();

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

        foreach($sku->prices as $price => $amount){
            $skuItem[] = $amount;
        }

        $skuItem[] = strip_tags(html_entity_decode($sku->sku_info->item->desc));

        foreach ($sku->sku_info->details as $details) {

            $skuItem[] = $details->details1;
            $skuItem[] = $details->details2;
            $skuItem[] = $details->details3;

        }

        $groupItem["skus"][] = $skuItem;
    }

    $allgroupResult[] = $groupItem;
}
Related