PHP 数组对象多级字段分组

PHP 数组对象多级字段分组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* @description:根据数据
* @param {dataArr:需要分组的数据;keyStr:分组依据}
* @return: array
*/
protected function dataGroup(array $dataArr,$keyStr)
{
$newArr=[];
foreach ($dataArr as $k => $val) { //数据根据日期分组
$newArr[$val[$keyStr]][] = $val;
}
return $newArr;
}

$list = array(
array(
'ctime'=>'2019-09-05 15:12:11',
'openid'=>'1',
),
array(
'ctime'=>'2012-09-05 15:12:11',
'openid'=>'2',
),
);
/** 1.根据时间分组*/
$list= $this->dataGroup($list,'ctime');
/** 2.根据openid分组*/
foreach($list as $k => $item){
$list[$k]= $this->dataGroup($item,'openid');
}

/**
json $list
{
2019-09-05 15:12:11:{
1:{
'ctime':'2019-09-05 15:12:11',
'openid':'1',
}
},
2012-09-05 15:12:11:{
2:{
'ctime':'2012-09-05 15:12:11',
'openid':'2',
}
}
}
**/
0%