假设数据是
$data = array(
array(
'id'=>1,
'name'=>'s1',
'product_name'=>'p1',
'month'=>'jan,2018',
'quantity'=>12
),
array(
'id'=>2,
'name'=>'s2',
'product_name'=>'p2',
'month'=>'jan,2018',
'quantity'=>1
),
array(
'id'=>3,
'name'=>'s3',
'product_name'=>'p3',
'month'=>'feb,2018',
'quantity'=>12
),
array(
'id'=>4,
'name'=>'s4',
'product_name'=>'p4',
'month'=>'feb,2018',
'quantity'=>12
),
array(
'id'=>4,
'name'=>'s4',
'product_name'=>'p4',
'month'=>'Mar,2018',
'quantity'=>12
)
);
创建动态标题
$header = array();
foreach($data as $val){
$header = array_unique(array_values(array_merge($header,array_keys($val))));
}
添加关键月份值作为带有数量值的键
foreach($data as $key=>$val){
$data[$key][$val['month']] = $val['quantity'];
}