У меня есть ряд студентов, которые зачислены для обучения, которые мне нужно отобразить, сгруппированные по дате их обучения, а затем по организации.
Но меня это совсем озадачило.Массив выглядит примерно так:
$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" )
);
В этом примере есть 3 даты, 5 компаний и 18 студентов. Я пытаюсь их вложить, создавая даты и резервируя массивы из основного массива заранее для созданияследующий результат:
<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>
Но, конечно, мое элементарное знание PHP подвело меня, и я просто не понимаю его.Я просто потерян.
Мой ужасный код в настоящее время выглядит так:
$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>';
}
}
}
Любая помощь будет принята с благодарностью.