title: PHP - Loop and merge array based on same Key
body: <pre><code>foreach ($total_month_hours_result as $key =&gt; $data){ print_r($data); } //outputs below array
  
  Array
  (
      [0] =&gt; Array
          (
              [SITE_STATUS] =&gt; Offshore
              [ACTUAL_HOURS] =&gt; 176
              [MONTH_YEAR] =&gt; APR-2015
          )
  
      [1] =&gt; Array
          (
              [SITE_STATUS] =&gt; Onsite
              [ACTUAL_HOURS] =&gt; 180
              [MONTH_YEAR] =&gt; APR-2015
          )
  
  )
  </code></pre>
  
  <p>Again looping the array</p>
  
  <pre><code>foreach ($data as $key =&gt; $val ){print_r($val);} // outputs below array
  
  
  Array
  (
      [SITE_STATUS] =&gt; Offshore
      [ACTUAL_HOURS] =&gt; 176
      [MONTH_YEAR] =&gt; APR-2015
  )
  
  Array
  (
      [SITE_STATUS] =&gt; Onsite
      [ACTUAL_HOURS] =&gt; 180
      [MONTH_YEAR] =&gt; APR-2015
  )
  </code></pre>
  
  <p>Tried below code</p>
  
  <pre><code>$hours_array = Array();
  foreach ($project_months as $month) {
  
      foreach ($total_month_hours_result as $key =&gt; $data){
  
          if (!empty($data)){
  
              foreach ($data as $key =&gt; $val ){
  
                  if ($val['MONTH_YEAR'] == $month ){
  
                    $hours_array[$val['MONTH_YEAR']][$month]['SITE_STATUS'] = $val['SITE_STATUS'];
                    $hours_array[$val['MONTH_YEAR']][$month]['OFFSHORE_HOURS'] = $val['OFFSHORE_HOURS'];
                    $hours_array[$val['MONTH_YEAR']][$month]['ONSITE_HOURS'] = $val['ONSITE_HOURS'];
                    $hours_array[$val['MONTH_YEAR']][$month]['TOTAL_HOURS'] = $val['ONSITE_HOURS'] + $val['OFFSHORE_HOURS'];
  
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Outputs</p>
  
  <pre><code>Array
  (
      [APR-2015] =&gt; Array
          (
              [APR-2015] =&gt; Array
                  (
                      [SITE_STATUS] =&gt; Onsite
                      [TOTAL_HOURS] =&gt; 356
                      [ONSITE_HOURS] =&gt; 180
                      [OFFSHORE_HOURS] =&gt; 176
                  )
  
          )       
  )
  </code></pre>
  
  <p>Based on key <code>month_year</code> how do I merge two arrays resulting into below output</p>
  
  <pre><code>Array
  (
      [SITE_STATUS] =&gt; Offshore
      [ACTUAL_HOURS] =&gt; 176
      [SITE_STATUS] =&gt; Onsite
      [ACTUAL_HOURS] =&gt; 180
      [MONTH_YEAR] =&gt; APR-2015
  )
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>What I am actually trying to achieve is to show Onsite, Offshore and Total Hours in table form like below</p>
  
  <p>It does display the hours correctly for the first APR-2015 but not for JUN-15</p>
  
  <pre><code>Name of Engineer    Site  MAR-15  Site    APR-15  Site     JUN-15
  User 1                            Onsite   120    Offshore  170              
  User 2                            Offshore 140    Offshore  180
  
  Total Hours                                260                                          350
  Total Onsite                               120                                          0
  Total Offshore                             140                                          350
  </code></pre>
  
  <p><strong>SQL:</strong></p>
  
  <pre><code>SELECT  decode(pr.siteid,   397,   'Onsite',   398,   'Offshore') AS
  site_status,
    SUM(tm.user_hours) AS
  actual_hours,
    tm.month_year
  FROM project_resource pr
  INNER JOIN monthly_table tm ON tm.resource_id = pr.employeeid
   AND tm.project_id = pr.projectid
  INNER JOIN resource_table rd ON rd.employeeid = pr.employeeid
  INNER JOIN expband_view exbvw ON exbvw.employeeid = pr.employeeid
  INNER JOIN mastercode_table mc ON mc.codeid = rd.primaryskill
  WHERE tm.project_id = 741
   AND tm.month_year = 'APR-2015' --months will be passed dynamically
  GROUP BY pr.siteid,tm.month_year
  ORDER BY SITE_STATUS DESC
  </code></pre>
  
  <p><strong>SQL Result</strong></p>
  
  <pre><code>SITE_STATUS  ACTUAL_HOURS   MONTH_YEAR
  Onsite        307           APR-2015
  Offshore      352           APR-2015
  </code></pre>