如题:直接先上代码:
$arr = array();
foreach($data as $v){
//前边order_id和time是按照需求拼接的一个键
$order_id = $v['order_id'];
$time = $['virtual_time'];
$sort = $order_id.$time;
//此时利用拼接好的键进行最外层数据的筛选
$arr[$sort]['order_id'] = $v['order_id'];
$arr[$sort]['order_title'] = $v['order_title'];
$arr[$sort]['order_money'] = $v['order_money'];
//此时进行数据第二层的组合
$temp_data['order_money'] = $v['order_money'];
//将组合好的第二层数据放入$arr中
$arr[$sort]['sort'] = $temp_data;
}
方法二:
$arr = array
(
0 => array
(
'initial' => 'A',
'typename' => '奥迪'
),
1 => array
(
'initial' => 'F',
'typename' =>'F1'
),
2 => array
(
'initial' => 'F',
'typename' => 'F2'
)
);
//分组后的数组
$result = array();
foreach($arr as $k=>$v){
$result[$v['initial']][] = $v;
}
- 虽然写法不同,但是意思相同,今天受益匪浅,学习了,很巧妙
网友评论