php键值相同的项数值相加

 php 合并一个二维数组相同项,数量则相加

 $arr = array(
     array(
        'user_id' => 100,
        'goods_id' => 10,
        'number' => 1,
     ),
     array(
        'user_id' => 100,
        'goods_id' => 10,
        'number' => 2,
     ),
     array(
        'user_id' => 100,
        'goods_id' => 10,
        'number' => 2,
     ),
     array(
        'user_id' => 100,
        'goods_id' => 10,
        'number' => 2,
     ),
 );
 理想结果:
 $arr = array(
     array(
         'user_id'=> 100,
         'goods_id' => 10,
         'number' => 7,
     ),

 );

 foreach($arr as $k=>$v){
     if(!isset($item[$v['user_id']])){
         $item[$v['user_id']]=$v;
     }else{
         $item[$v['user_id']]['number']+=$v['number'];
     }
 }
 print_r($item);
上一篇:ArcGIS Desktop10.2与CityEngine2012兼容问题


下一篇:错误 1 未能找到元数据文件“C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/wwwroot/7cb4fcd