title: PHP Group/Nest Array Results
body: <p>I have an array of students who are booked in for training that I need to display, grouped by their training date, then their organisation. </p>
  
  <p>But it's got me completely stumped. 
  The array looks something like this: </p>
  
  <pre><code>$bookings = 
  array(
      array( "Date"=&gt;"12/05/2019","Company"=&gt;"Company 1","Student"=&gt;"Student 1" ),
      array( "Date"=&gt;"12/05/2019","Company"=&gt;"Company 1","Student"=&gt;"Student 2" ),
      array( "Date"=&gt;"12/05/2019","Company"=&gt;"Company 1","Student"=&gt;"Student 3" ),
      array( "Date"=&gt;"12/05/2019","Company"=&gt;"Company 1","Student"=&gt;"Student 4" ),
      array( "Date"=&gt;"12/05/2019","Company"=&gt;"Company 1","Student"=&gt;"Student 5" ),
      array( "Date"=&gt;"12/05/2019","Company"=&gt;"Company 2","Student"=&gt;"Student 6" ),
      array( "Date"=&gt;"12/05/2019","Company"=&gt;"Company 2","Student"=&gt;"Student 7" ),
      array( "Date"=&gt;"12/05/2019","Company"=&gt;"Company 2","Student"=&gt;"Student 8" ),
      array( "Date"=&gt;"19/05/2019","Company"=&gt;"Company 3","Student"=&gt;"Student 9" ),
      array( "Date"=&gt;"19/05/2019","Company"=&gt;"Company 3","Student"=&gt;"Student 10" ),
      array( "Date"=&gt;"19/05/2019","Company"=&gt;"Company 3","Student"=&gt;"Student 11" ),
      array( "Date"=&gt;"19/05/2019","Company"=&gt;"Company 3","Student"=&gt;"Student 12" ),
      array( "Date"=&gt;"26/05/2019","Company"=&gt;"Company 4","Student"=&gt;"Student 13" ),
      array( "Date"=&gt;"26/05/2019","Company"=&gt;"Company 4","Student"=&gt;"Student 14" ),
      array( "Date"=&gt;"26/05/2019","Company"=&gt;"Company 4","Student"=&gt;"Student 15" ),
      array( "Date"=&gt;"26/05/2019","Company"=&gt;"Company 5","Student"=&gt;"Student 16" ),
      array( "Date"=&gt;"26/05/2019","Company"=&gt;"Company 5","Student"=&gt;"Student 17" ),
      array( "Date"=&gt;"26/05/2019","Company"=&gt;"Company 5","Student"=&gt;"Student 18" )
  );
  </code></pre>
  
  <p>In this example, there are 3 dates, 5 companies and 18 Students
  I'm trying to nest them by creating date and booking arrays from the main array beforehand to produce the following result: </p>
  
  <pre><code>&lt;div class="Date"&gt;Bookings for 12/05/2019&lt;/div&gt;
      &lt;div class="Company"&gt;Booked for Company 1&lt;/div&gt;
          &lt;div class="Student"&gt;Student 1&lt;/div&gt;
          &lt;div class="Student"&gt;Student 2&lt;/div&gt;
          &lt;div class="Student"&gt;Student 3&lt;/div&gt;
          &lt;div class="Student"&gt;Student 4&lt;/div&gt;
          &lt;div class="Student"&gt;Student 5&lt;/div&gt;
      &lt;div class="Company"&gt;Booked for Company 2&lt;/div&gt;
          &lt;div class="Student"&gt;Student 6&lt;/div&gt;
          &lt;div class="Student"&gt;Student 7&lt;/div&gt;
          &lt;div class="Student"&gt;Student 8&lt;/div&gt;
  &lt;div class="Date"&gt;Bookings for 19/05/2019&lt;/div&gt;
      &lt;div class="Company"&gt;Booked for Company 3&lt;/div&gt;
          &lt;div class="Student"&gt;Student 9&lt;/div&gt;
          &lt;div class="Student"&gt;Student 10&lt;/div&gt;
          &lt;div class="Student"&gt;Student 11&lt;/div&gt;
          &lt;div class="Student"&gt;Student 12&lt;/div&gt;
  &lt;div class="Date"&gt;Bookings for 26/05/2019&lt;/div&gt;
      &lt;div class="Company"&gt;Booked for Company 4&lt;/div&gt;
          &lt;div class="Student"&gt;Student 13&lt;/div&gt;
          &lt;div class="Student"&gt;Student 14&lt;/div&gt;
          &lt;div class="Student"&gt;Student 15&lt;/div&gt;
      &lt;div class="Company"&gt;Booked for Company 5&lt;/div&gt;
          &lt;div class="Student"&gt;Student 16&lt;/div&gt;
          &lt;div class="Student"&gt;Student 17&lt;/div&gt;
          &lt;div class="Student"&gt;Student 18&lt;/div&gt;
  </code></pre>
  
  <p>But, of course, my rudimentary knowledge of PHP has failed me miserably and I am just not getting it. I'm just lost. </p>
  
  <p>My horrid code currently looks like this: </p>
  
  <pre><code>$by_date = array();
  
  foreach( $bookings as $dte )
      {
          $by_date['Date'][] = $dte['Date'];
      }
  
  $by_company = array();
  
  foreach( $bookings as $comp )
      {
          $by_company['Company'][] = $comp['Company'];
      }
  
  
  foreach($by_date as $did =&gt; $company_array)
  {
      $b_date = $company_array[0]['Date'];
      echo '&lt;div class="Date"&gt;Bookings for '.$b_date.'&lt;/div&gt;';
          foreach( $by_company as $cid =&gt; $student_array )
          {
              $c_name = $student_array[0]['Company'];
              echo '&lt;div class="Company"&gt;Bookings for '.$c_name.'&lt;/div&gt;';
  
              foreach( $student_array as $student )
              {
                  $stud = $student[0]['Student'];
                  echo '&lt;div class="Student"&gt;'.$stud.'&lt;/div&gt;';
              }
          }
  }
  </code></pre>
  
  <p>Any help at all would be greatly appreciated. </p>