本文实例讲述了php数组实现根据某个键值将相同键值合并生成新二维数组的方法。分享给大家供大家参考,具体如下:
源数据:
$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相同的二维数组合并到一个数组中,生成一个新的数组
代码:
<?php
$result= array();
foreach ($infos as $key => $info) {
$result[$info['a']][] = $info;
}
print_r($result);
输出:
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相关内容感兴趣的读者可查看本站专题:《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php常用函数与技巧总结》、《PHP错误与异常处理方法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
您可能感兴趣的文章:php对二维数组按指定键值key排序示例代码PHP二维数组实现去除重复项的方法【保留各个键值】PHP按指定键值对二维数组进行排序的方法php二维数组按某个键值排序的实例讲解php自定义排序uasort函数示例【二维数组按指定键值排序】php二维数组用键名分组相加实例函数详解PHP 二维数组排序保持键名不变PHP实现二维数组按指定的键名排序的方法示例PHP实现通过二维数组键值获取一维键名操作示例