Php 按数组\u push计数行
我有点困在这里了 我有一个用laravel excel导出的数组,我基本上是创建一个类别行,然后创建属于该类别的所有后续项目行 如何为groupItem数组中的每个数组\u push添加一个计数器,以便计算groupItem push之间的每一行,并将类别信息的行设置为粗体 基本上,我只想加粗包含category_code、category_name和category_desc数据的行,因此我需要根据数组_push对我相信的category info进行迭代 我想我需要设置一个计数,增加categoryItem数组的计数,将该计数存储在一个数组中,然后将这些数组存储的行设置为粗体Php 按数组\u push计数行,php,arrays,phpexcel,laravel-excel,Php,Arrays,Phpexcel,Laravel Excel,我有点困在这里了 我有一个用laravel excel导出的数组,我基本上是创建一个类别行,然后创建属于该类别的所有后续项目行 如何为groupItem数组中的每个数组\u push添加一个计数器,以便计算groupItem push之间的每一行,并将类别信息的行设置为粗体 基本上,我只想加粗包含category_code、category_name和category_desc数据的行,因此我需要根据数组_push对我相信的category info进行迭代 我想我需要设置一个计数,增加cate
$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"
}
}
}
然后,只要您需要获得每个类别中的产品数量,就可以进行计数($item['sku'])
。为此,请尝试对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;
}
那么,我该如何准确地计算需要加粗的类别行呢?我以为您想要加粗包含SKU的类别行。如果这是正确的,那么类似于
If(count($groupItem[“skus”]){//突出显示此行}
这样的逻辑是否适用于laravel excel?另外,我还没有测试这个,但我不确定数组结构是否能正确导出到excel,但我会尝试,我会让你知道的