標籤:
要處理的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‘ => ));
處理要求:將數組中gid相同的二維數組合并到一個數組中,產生一個新的二維數組
代碼:
$result= array();foreach ($infos as $key => $info) { $result[$info[‘gid‘]][] = $info;} print_r($result);
output:
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數組根據某索引值,把相同索引值的合并最終產生一個新的二維數組