PHP根据数组的值分组,php array中没有自带这个函数但是很常用,今天写了出来记录一下。
代码:
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
|
$_array
=
array
(
array
(1,11,
'2016-05-18'
),
array
(2,11,
'2016-05-18'
),
array
(3,22,
'2016-05-18'
),
array
(4,22,
'2016-05-18'
),
array
(5,33,
'2016-05-19'
),
array
(6,33,
'2016-05-19'
),
array
(7,44,
'2016-05-19'
),
array
(8,44,
'2016-05-19'
),
array
(9,55,
'2016-05-20'
),
array
(10,55,
'2016-05-20'
),
);
var_dump(array_val_chunk(
$_array
));
function
array_val_chunk(
$array
){
$result
=
array
();
foreach
(
$array
as
$key
=>
$value
) {
$result
[
$value
[1].
$value
[2]][] =
$value
;
}
$ret
=
array
();
//这里把简直转成了数字的,方便同意处理
foreach
(
$result
as
$key
=>
$value
) {
array_push
(
$ret
,
$value
);
}
return
$ret
;
}
|
运行结果如下:
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
array
(5) {
[0]=>
array
(2) {
[0]=>
array
(3) {
[0]=>
int(1)
[1]=>
int(11)
[2]=>
string(10)
"2016-05-18"
}
[1]=>
array
(3) {
[0]=>
int(2)
[1]=>
int(11)
[2]=>
string(10)
"2016-05-18"
}
}
[1]=>
array
(2) {
[0]=>
array
(3) {
[0]=>
int(3)
[1]=>
int(22)
[2]=>
string(10)
"2016-05-18"
}
[1]=>
array
(3) {
[0]=>
int(4)
[1]=>
int(22)
[2]=>
string(10)
"2016-05-18"
}
}
[2]=>
array
(2) {
[0]=>
array
(3) {
[0]=>
int(5)
[1]=>
int(33)
[2]=>
string(10)
"2016-05-19"
}
[1]=>
array
(3) {
[0]=>
int(6)
[1]=>
int(33)
[2]=>
string(10)
"2016-05-19"
}
}
[3]=>
array
(2) {
[0]=>
array
(3) {
[0]=>
int(7)
[1]=>
int(44)
[2]=>
string(10)
"2016-05-19"
}
[1]=>
array
(3) {
[0]=>
int(8)
[1]=>
int(44)
[2]=>
string(10)
"2016-05-19"
}
}
[4]=>
array
(2) {
[0]=>
array
(3) {
[0]=>
int(9)
[1]=>
int(55)
[2]=>
string(10)
"2016-05-20"
}
[1]=>
array
(3) {
[0]=>
int(10)
[1]=>
int(55)
[2]=>
string(10)
"2016-05-20"
}
}
}
|