title: Aggregating an array and displaying the aggregation result alongside the detail
body: <p>I have a <code>$department</code> variable, which is an instance of a <code>Department</code> class.</p>
  
  <p>The <code>Department</code> class has a property called <code>$employees</code> that contains an array of <code>Employee</code> instances related to that department (retrieved from the database).</p>
  
  <p>The <code>Employee</code> class has a properties called <code>$salary_amount</code> and <code>$job_type_id</code>. The <code>$employees</code> array elements are ordered together by <code>$job_type_id</code>.</p>
  
  <p>For a specified department, I'm outputting a list of employees in that department like so:</p>
  
  <pre><code>Employees for IT Department
  ----------------------------
  
  &lt;?php foreach($department-&gt;employees as $employee): ?&gt;
    &lt;?php echo $employee-&gt;name; ?&gt;
    &lt;?php echo $employee-&gt;salary_amount; ?&gt;
  &lt;?php endforeach; ?&gt;
  </code></pre>
  
  <p><strong>What's an efficient way to manipulate</strong> <code>$department-&gt;employees</code> <strong>so I can display a sum-total of the</strong> <code>$salary_amount</code> <strong>for each</strong> <code>$job_type_id</code> <strong>alongside the list of employees?</strong> </p>
  
  <pre><code>Employees for IT Department
  ----------------------------
  
  ## Help Desk ##
  
  Joseph Lazar
  $47,000
  
  John Q. Smith
  $15,444
  
  --&gt; Total Salary for Help Desk: $62,444
  
  
  ## Database Administrator ##
  
  Piet Pietersen
  $55,120
  
  Ivan Horvat
  $63,750
  
  --&gt; Total Salary for Help Desk: $118,870
  </code></pre>
  
  <p><strong>Current Approach</strong></p>
  
  <p>Currently, <code>$employees</code> is being populated by a single SQL query, ordering the results how I want. I'm then running a separate SQL query to get an array of <code>$salary</code> sum-totals for each <code>$job_type_id</code> using a technique from <a href="https://stackoverflow.com/questions/11394954/grouped-aggregations-with-yii-stat">another question</a>.</p>
  
  <p>However, the resultset from this 2nd query is stored in a different variable <code>$subtotals</code>. Therefore, I still have the problem of efficiently getting the Sum-Total to display "in-context" with the list of employees. </p>
  
  <p>I happen to be using <a href="http://www.yiiframework.com" rel="nofollow noreferrer">Yii framework</a>, however that's probably irrelevant unless I should think about or approach this type of problem from a totally different perspective? Please tell me in the comments.</p>
  
  <p><strong>Update 1</strong></p>
  
  <p>Here's the current code I'm using, however it feels messy and like there could be a better way. As mentioned above, the aggregation is done by a separate SQL, however I'm open to alternatives.</p>
  
  <pre><code>&lt;?php 
  
  $employee_key = 0;
  $employees_count = sizeof($department-&gt;employees);
  
  $type_sum_key = 0;
  $current_type_in_focus = null;
  $next_type_in_focus = null;
  
  foreach($department-&gt;employees as $employee)
  {
    $current_type_in_focus = $employee-&gt;job_type_id;
    $next_type_in_focus = (isset($department-&gt;employees[$employee_key+1])) ? $department-&gt;employees[$employee_key+1]-&gt;job_type_id : null;
  
    # output individual employee data
    echo $employee-&gt;name;
    echo $employee-&gt;salary_amount;
  
    # output aggregated total for job type
    if ( $current_type_in_focus !== $next_type_in_focus || $employee_key+1 == $employees_count )
    {
      echo '--&gt; Total Salary for ' . $employee-&gt;jobType-&gt;name . ': ' . $department-&gt;subtotals[$type_sum_key]-&gt;total;
      $type_sum_key++; 
    }
  
    $income_key++;
  }
  
  ?&gt;
  </code></pre>