Merge and group by several arrays

前端 未结 3 1937
陌清茗
陌清茗 2021-01-24 03:55

I need to merge associative arrays and group by the name. Say I have such 3 arrays:

ARRAY1
    \"/path/file.jpg\"  =>  2, 
    \"/path/file2.bmp\" =>  1,
          


        
3条回答
  •  时光说笑
    2021-01-24 04:53

    one possible way

    $rtn = array();
    foreach ($array1 as $key=>$val)
    {
      $rtn[$key]+=$val;
    }
    
    foreach ($array2 as $key=>$val)
    {
      $rtn[$key]+=$val;
    }
    
    foreach ($array2 as $key=>$val)
    {
      $rtn[$key]+=$val;
    }
    

    the above will assign the filename, SUM(val) as an associative array into $rtn

提交回复
热议问题