title: PHP: Separate values in foreach according to key
body: <p>I am using PHP Laravel.</p>
  
  <p>I need to get the total time/hours of each employee. Each employee has an assigned timeKeeping_empID.
  the Time_Logs table has the time logs of the Employees with timeKeeping_empID as the foreign key.</p>
  
  <p>I tried running my code:
  Controller: </p>
  
  <pre><code>function getTotalHours(Request $request)
          {
  
              $team = Employees::where('managerID', $request-&gt;session()-&gt;get('employeeID'))-&gt;get();
                  //I am assigning an array to be grouped according to my employeeID session.
                  $teamIDs = array();
                  //I am getting my own session so I can see my data as well together with my team
                  array_push($teamIDs, $request-&gt;session()-&gt;get('employeeID'));
                  foreach($team as $member) {
                      array_push($teamIDs, $member-&gt;employeeID);
                  }
                  //I am getting the column employeeID which is the same with the teamID
                  $empData = Employees::whereIn('employeeID', $teamIDs)-&gt;get();
                  $timeKeepingIDs = array();
                  foreach($empData as $anEmpData) {
                      array_push($timeKeepingIDs, $anEmpData-&gt;timeKeeping_empID);
                  }
                  $timeLogData = Time_Logs::whereIn('timeKeeping_empID', $timeKeepingIDs)-&gt;get();
  
              return view('welcome', compact('timeLogData','request'));
          }
  </code></pre>
  
  <p>in my view:
  $totalHours=0;</p>
  
  <pre><code>        foreach ($timeLogData as $timeLogDatas) {
  
          $dateToday = new DateTime($timeLogDatas-&gt;date);
          $timeFrom = date_create($timeLogDatas-&gt;time_in);
          $timeTo = date_create($timeLogDatas-&gt;time_out);
          $hoursRendered = date_diff($timeFrom, $timeTo);
  
          $totalHours=$totalHours += (int) $hoursRendered-&gt;format('%h');
          }
          echo "$totalHours";
  </code></pre>
  
  <p>but what comes out is the Total Hours of ALL the Employee.
  What I need is the total hours of Each employee.</p>
  
  <p>Here is my database table:  </p>
  
  <pre><code>(Employee) 
  EmployeeID (primaryKey) 
  timeKeeping_empID 
  name
  
  
  (Time_Logs)
  id(primaryKey)
  timeKeeping_empID
  date
  time-in
  time-out
  </code></pre>
  
  <p>Sorry, I'm a newbie in PHP.</p>