1、新建php文件。
2、创建名为$a1的数组并为数组添加元素。
3、创建名为$a2的数组并为数组添加元素。
4、创建名为$a3的数组并为数组添加元素。
5、array_diff_assoc() 比较三个数组的键名和键值,并返回差集。
6、打印差集。
7、预览效果如图。
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'
)
);1234567891011121314151617181920212223242526272829303132333435363738394041424344
处理要求:将数组中a相同的二维数组合并到一个数组中,生成一个新的数组
代码:
$result= array(); foreach ($infos as $key => $info) { $result[$info['a']][] = $info;
}
print_r($result);123456
输出:
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
)
)
)
foreach($sourceArray as $k=>$v) {
$result[$v["sendto"]][] = $v; // sendto 根据你想要的
}