按 array_push 计算行数
Counting rows by array_push
我有点卡在这里
我有一个要使用 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');
为什么不在 $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;
}
我有点卡在这里
我有一个要使用 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');
为什么不在 $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;
}