原始数据
$arr = [
['attr_id'=>1,'attr_name'=>'尺寸','min_id'=>3,'attr_value'=>'大'],
['attr_id'=>1,'attr_name'=>'尺寸','min_id'=>5,'attr_value'=>'小'],
['attr_id'=>1,'attr_name'=>'尺寸','min_id'=>2,'attr_value'=>'中'],
['attr_id'=>2,'attr_name'=>'颜色','min_id'=>4,'attr_value'=>'红'],
['attr_id'=>2,'attr_name'=>'颜色','min_id'=>1,'attr_value'=>'绿'],
['attr_id'=>2,'attr_name'=>'颜色','min_id'=>6,'attr_value'=>'蓝']
];
预期目标:实现将相同属性归类,同时将属于该属性的属性值归到该分组中
$r = [];
foreach ($arr as $item){
//将属性归类
$r[$item['attr_id']]['attr_info'] = [
'attr_id'=>$item['attr_id'],
'attr_name'=>$item['attr_name'],
];
//将属性值同时归类
$r[$item['attr_id']]['value_info'][] = [
'min_id'=>$item['min_id'],
'attr_value'=>$item['attr_value']
];
}
$t = array_values($r);//将数组下标重置,避免接口数组给前端出现意外情况
效果
Json视图效果
网友评论