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.
But it's got me completely stumped. The array looks something like this:
$bookings =
array(
array( "Date"=>"12/05/2019","Company"=>"Company 1","Student"=>"Student 1" ),
array( "Date"=>"12/05/2019","Company"=>"Company 1","Student"=>"Student 2" ),
array( "Date"=>"12/05/2019","Company"=>"Company 1","Student"=>"Student 3" ),
array( "Date"=>"12/05/2019","Company"=>"Company 1","Student"=>"Student 4" ),
array( "Date"=>"12/05/2019","Company"=>"Company 1","Student"=>"Student 5" ),
array( "Date"=>"12/05/2019","Company"=>"Company 2","Student"=>"Student 6" ),
array( "Date"=>"12/05/2019","Company"=>"Company 2","Student"=>"Student 7" ),
array( "Date"=>"12/05/2019","Company"=>"Company 2","Student"=>"Student 8" ),
array( "Date"=>"19/05/2019","Company"=>"Company 3","Student"=>"Student 9" ),
array( "Date"=>"19/05/2019","Company"=>"Company 3","Student"=>"Student 10" ),
array( "Date"=>"19/05/2019","Company"=>"Company 3","Student"=>"Student 11" ),
array( "Date"=>"19/05/2019","Company"=>"Company 3","Student"=>"Student 12" ),
array( "Date"=>"26/05/2019","Company"=>"Company 4","Student"=>"Student 13" ),
array( "Date"=>"26/05/2019","Company"=>"Company 4","Student"=>"Student 14" ),
array( "Date"=>"26/05/2019","Company"=>"Company 4","Student"=>"Student 15" ),
array( "Date"=>"26/05/2019","Company"=>"Company 5","Student"=>"Student 16" ),
array( "Date"=>"26/05/2019","Company"=>"Company 5","Student"=>"Student 17" ),
array( "Date"=>"26/05/2019","Company"=>"Company 5","Student"=>"Student 18" )
);
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:
<div class="Date">Bookings for 12/05/2019</div>
<div class="Company">Booked for Company 1</div>
<div class="Student">Student 1</div>
<div class="Student">Student 2</div>
<div class="Student">Student 3</div>
<div class="Student">Student 4</div>
<div class="Student">Student 5</div>
<div class="Company">Booked for Company 2</div>
<div class="Student">Student 6</div>
<div class="Student">Student 7</div>
<div class="Student">Student 8</div>
<div class="Date">Bookings for 19/05/2019</div>
<div class="Company">Booked for Company 3</div>
<div class="Student">Student 9</div>
<div class="Student">Student 10</div>
<div class="Student">Student 11</div>
<div class="Student">Student 12</div>
<div class="Date">Bookings for 26/05/2019</div>
<div class="Company">Booked for Company 4</div>
<div class="Student">Student 13</div>
<div class="Student">Student 14</div>
<div class="Student">Student 15</div>
<div class="Company">Booked for Company 5</div>
<div class="Student">Student 16</div>
<div class="Student">Student 17</div>
<div class="Student">Student 18</div>
But, of course, my rudimentary knowledge of PHP has failed me miserably and I am just not getting it. I'm just lost.
My horrid code currently looks like this:
$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 => $company_array)
{
$b_date = $company_array[0]['Date'];
echo '<div class="Date">Bookings for '.$b_date.'</div>';
foreach( $by_company as $cid => $student_array )
{
$c_name = $student_array[0]['Company'];
echo '<div class="Company">Bookings for '.$c_name.'</div>';
foreach( $student_array as $student )
{
$stud = $student[0]['Student'];
echo '<div class="Student">'.$stud.'</div>';
}
}
}
Any help at all would be greatly appreciated.