php数组根据某键值,把相同键值的合并最终生成一个新的二维数组
Posted zwb21fang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php数组根据某键值,把相同键值的合并最终生成一个新的二维数组相关的知识,希望对你有一定的参考价值。
$infos = array( array( ‘gid‘ => 36, ‘name‘ => ‘高二佳木斯‘, ‘start_time‘ => ‘2015-08-28 00:00:00‘, ‘pic‘ => ‘2015/08/438488a00b3219929282e3652061c2e3.png‘ ), array( ‘gid‘ => 36, ‘name‘ => ‘高二佳木斯‘, ‘start_time‘ => ‘2015-08-20 00:00:00‘, ‘pic‘ => ‘2015/08/438488a00b3219929282e3652061c2e3.png‘ ), array( ‘gid‘ => 36, ‘name‘ => ‘高二佳木斯‘, ‘start_time‘ => ‘2015-08-28 00:00:00‘, ‘pic‘ => ‘2015/08/438488a00b3219929282e3652061c2e3.png‘ ), array( ‘gid‘ => 36, ‘name‘ => ‘高二佳木斯‘, ‘start_time‘ => ‘2015-08-27 00:00:00‘, ‘pic‘ => ‘2015/08/438488a00b3219929282e3652061c2e3.png‘ ), array( ‘gid‘ => 18, ‘name‘ => ‘天书‘, ‘start_time‘ => ‘2015-08-24 00:00:00‘, ‘pic‘ => ‘dev/2015/08/438488a00b3219929282e3652061c2e3.png‘ ), array( ‘gid‘ => 17, ‘name‘ => ‘晒黑西游‘, ‘start_time‘ => ‘2015-08-06 00:00:00‘, ‘pic‘ => ‘‘ ) array( ‘gid‘ => 17, ‘name‘ => ‘晒黑西游‘, ‘start_time‘ => ‘2015-08-24 00:00:00‘, ‘pic‘ => ) );
$result= array(); foreach ($infos as $key => $info) { $result[$info[‘gid‘]][] = $info; } print_r($result);
Array( [36] => Array( [0] => Array( [gid] => 36 [name] => 高二佳木斯 [start_time] => 2015-08-28 00:00:00 [pic] => dev/2015/08/438488a00b3219929282e3652061c2e3.png ) [1] => Array( [gid] => 36 [name] => 高二佳木斯 [start_time] => 2015-08-20 00:00:00 [pic] => dev/2015/08/438488a00b3219929282e3652061c2e3.png ) [2] => Arra( [gid] => 36 [name] => 高二佳木斯 [start_time] => 2015-08-28 00:00:00 [pic] => dev/2015/08/438488a00b3219929282e3652061c2e3.png ) [3] => Array( [gid] => 36 [name] => 高二佳木斯 [start_time] => 2015-08-27 00:00:00 [pic] => dev/2015/08/438488a00b3219929282e3652061c2e3.png ) ) [18] => Array( [0] => Array( [gid] => 18 [name] => 天书 [start_time] => 2015-08-24 00:00:00 [pic] => dev/2015/08/438488a00b3219929282e3652061c2e3.png ) ) [17] => Array( [0] => Arra( [gid] => 17 [name] => 晒黑西游 [start_time] => 2015-08-06 00:00:00 [pic] => ) [1] => Array( [gid] => 17 [name] => 晒黑西游 [start_time] => 2015-08-24 00:00:00 [pic] => ) ) );
以上是关于php数组根据某键值,把相同键值的合并最终生成一个新的二维数组的主要内容,如果未能解决你的问题,请参考以下文章
php数组实现根据某个键值将相同键值合并生成新二维数组的方法
php 二维数组中取某个唯一的键值为键名。(通过合并两个数组来创建一个新数组,其中的一个数组元素为键名,另一个数组的元素为键值。)