本文实例讲述了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
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 (3) {
[0]=>
array (16) {
[ "id" ]=>
string(2) "42"
[ "uid" ]=>
string(2) "14"
[ "euid" ]=>
string(2) "56"
[ "did" ]=>
string(1) "1"
[ "nid" ]=>
string(1) "0"
[ "phonetime" ]=>
string(10) "1443927600"
[ "createtime" ]=>
string(10) "1443880619"
[ "type" ]=>
string(1) "3"
[ "status" ]=>
string(1) "0"
[ "atype" ]=>
string(1) "1"
[ "mtype" ]=>
string(1) "2"
[ "endtime" ]=>
string(1) "0"
[ "time" ]=>
string(10) "10月04日"
[ "date" ]=>
string(6) "周日"
[ "uname" ]=>
NULL
[ "album" ]=>
string(0) ""
}
[1]=>
array (16) {
[ "id" ]=>
string(2) "40"
[ "uid" ]=>
string(2) "14"
[ "euid" ]=>
string(2) "56"
[ "did" ]=>
string(1) "1"
[ "nid" ]=>
string(1) "0"
[ "phonetime" ]=>
string(10) "1444359600"
[ "createtime" ]=>
string(10) "1444268595"
[ "type" ]=>
string(1) "3"
[ "status" ]=>
string(1) "0"
[ "atype" ]=>
string(1) "1"
[ "mtype" ]=>
string(1) "2"
[ "endtime" ]=>
string(1) "0"
[ "time" ]=>
string(10) "10月09日"
[ "date" ]=>
string(6) "周五"
[ "uname" ]=>
NULL
[ "album" ]=>
string(0) ""
}
[2]=>
array (16) {
[ "id" ]=>
string(2) "43"
[ "uid" ]=>
string(1) "2"
[ "euid" ]=>
string(2) "56"
[ "did" ]=>
string(1) "1"
[ "nid" ]=>
string(1) "0"
[ "phonetime" ]=>
string(10) "1444359620"
[ "createtime" ]=>
string(10) "1444268595"
[ "type" ]=>
string(1) "3"
[ "status" ]=>
string(1) "0"
[ "atype" ]=>
string(1) "1"
[ "mtype" ]=>
string(1) "2"
[ "endtime" ]=>
string(1) "0"
[ "time" ]=>
string(10) "10月09日"
[ "date" ]=>
string(6) "周五"
[ "uname" ]=>
NULL
[ "album" ]=>
string(0) ""
}
}
|
现在想要对这个二维数组下的元素,time相同的合并到同一数组下。想要的效果为:
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
108
109
110
111
112
113
|
array (2) {
[ "10月04日" ]=>
array (1) {
[0]=>
array (16) {
[ "id" ]=>
string(2) "42"
[ "uid" ]=>
string(2) "14"
[ "euid" ]=>
string(2) "56"
[ "did" ]=>
string(1) "1"
[ "nid" ]=>
string(1) "0"
[ "phonetime" ]=>
string(10) "1443927600"
[ "createtime" ]=>
string(10) "1443880619"
[ "type" ]=>
string(1) "3"
[ "status" ]=>
string(1) "0"
[ "atype" ]=>
string(1) "1"
[ "mtype" ]=>
string(1) "2"
[ "endtime" ]=>
string(1) "0"
[ "time" ]=>
string(10) "10月04日"
[ "date" ]=>
string(6) "周日"
[ "uname" ]=>
NULL
[ "album" ]=>
string(0) ""
}
}
[ "10月09日" ]=>
array (2) {
[0]=>
array (16) {
[ "id" ]=>
string(2) "40"
[ "uid" ]=>
string(2) "14"
[ "euid" ]=>
string(2) "56"
[ "did" ]=>
string(1) "1"
[ "nid" ]=>
string(1) "0"
[ "phonetime" ]=>
string(10) "1444359600"
[ "createtime" ]=>
string(10) "1444268595"
[ "type" ]=>
string(1) "3"
[ "status" ]=>
string(1) "0"
[ "atype" ]=>
string(1) "1"
[ "mtype" ]=>
string(1) "2"
[ "endtime" ]=>
string(1) "0"
[ "time" ]=>
string(10) "10月09日"
[ "date" ]=>
string(6) "周五"
[ "uname" ]=>
NULL
[ "album" ]=>
string(0) ""
}
[1]=>
array (16) {
[ "id" ]=>
string(2) "43"
[ "uid" ]=>
string(1) "2"
[ "euid" ]=>
string(2) "56"
[ "did" ]=>
string(1) "1"
[ "nid" ]=>
string(1) "0"
[ "phonetime" ]=>
string(10) "1444359620"
[ "createtime" ]=>
string(10) "1444268595"
[ "type" ]=>
string(1) "3"
[ "status" ]=>
string(1) "0"
[ "atype" ]=>
string(1) "1"
[ "mtype" ]=>
string(1) "2"
[ "endtime" ]=>
string(1) "0"
[ "time" ]=>
string(10) "10月09日"
[ "date" ]=>
string(6) "周五"
[ "uname" ]=>
NULL
[ "album" ]=>
string(0) ""
}
}
}
|
那么。。。代码很简单,没有想象的那么复杂,想要的结果是个三维数组
$result 为原二维数组
1
2
3
4
|
$res = array (); //想要的结果
foreach ( $result as $k => $v ) {
$res [ $v [ 'time' ]][] = $v ;
}
|
希望本文所述对大家PHP程序设计有所帮助。