答案 0 :(得分:0)
假设数据
$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'];
}