本文实例讲述了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
|
$infos = array (
array (
'a' => 36,
'b' => 'xa' ,
'c' => '2015-08-28 00:00:00' ,
'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
),
array (
'a' => 3,
'b' => 'vd' ,
'c' => '2015-08-20 00:00:00' ,
'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
),
array (
'a' => 6,
'b' => 'wwe' ,
'c' => '2015-08-28 00:00:00' ,
'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
),
array (
'a' => 36,
'b' => 'se' ,
'c' => '2015-08-28 00:00:00' ,
'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
),
array (
'a' => 6,
'b' => 'aw' ,
'c' => '2015-08-28 00:00:00' ,
'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
),
array (
'a' => 36,
'b' => 'bv' ,
'c' => '2015-08-28 00:00:00' ,
'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
),
array (
'a' => 12,
'b' => 'xx' ,
'c' => '2015-08-27 00:00:00' ,
'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
)
);
|
处理要求:将数组中a相同的二维数组合并到一个数组中,生成一个新的数组
代码:
1
2
3
4
5
6
|
<?php
$result = array ();
foreach ( $infos as $key => $info ) {
$result [ $info [ 'a' ]][] = $info ;
}
print_r( $result );
|
输出:
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
|
Array
(
[36] => Array
(
[0] => Array
(
[a] => 36
[b] => xa
[c] => 2015-08-28 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
[1] => Array
(
[a] => 36
[b] => se
[c] => 2015-08-28 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
[2] => Array
(
[a] => 36
[b] => bv
[c] => 2015-08-28 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
)
[3] => Array
(
[0] => Array
(
[a] => 3
[b] => vd
[c] => 2015-08-20 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
)
[6] => Array
(
[0] => Array
(
[a] => 6
[b] => wwe
[c] => 2015-08-28 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
[1] => Array
(
[a] => 6
[b] => aw
[c] => 2015-08-28 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
)
[12] => Array
(
[0] => Array
(
[a] => 12
[b] => xx
[c] => 2015-08-27 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
)
)
|
希望本文所述对大家PHP程序设计有所帮助。