码迷,mamicode.com
首页 > 编程语言 > 详细

php数组根据某键值,把相同键值的合并最终生成一个新的二维数组

时间:2015-08-20 10:57:43      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:

要处理的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数组根据某键值,把相同键值的合并最终生成一个新的二维数组

标签:

原文地址:http://my.oschina.net/u/2380669/blog/494899

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!