通过array_push计数行

时间:2019-01-18 07:33:53

标签: php arrays phpexcel laravel-excel

我有点卡在这里

我有一个要用laravel excel导出的数组,基本上是在创建类别行,然后创建所有属于该类别的项目行。

如何为每个array_push正确添加一个计数器到groupItem数组,以便我可以对groupItem推送之间的每一行进行计数,并将类别信息设置为粗体?

基本上,我只想将包含category_code,category_name和category_desc数据的行加粗,因此我需要根据array_push迭代获取我认为的类别信息

我想我需要设置一个计数,增加categoryItem array_push的计数,将该计数存储在数组中,然后将那些存储的行设置为粗体?

$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 个答案:

答案 0 :(得分:2)

为什么不为每个类别在$allgroupResult内创建另一个折叠数组,所以具有如下结构:

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

,然后只要需要获取每个类别中的产品数量就可以执行count($item['skus'])。为此,请尝试对foreach循环进行以下修改:

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