我需要显示一些要接受培训的学生,按他们的培训日期,然后按照他们的组织进行分组。
但这让我完全陷入了困境。 数组看起来像这样:
$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>';
}
}
}
任何帮助都将不胜感激。
答案 0 :(得分:1)
此技巧是:将当前数组更改为树数组。关键是数据,公司:
提示:在PHP 7中,您可以通过[]声明数组
$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" )
);
$result = [];
foreach($bookings as $itemBooking){
$result[$itemBooking["Date"]][$itemBooking["Company"]][]=$itemBooking["Student"];
}
foreach($result as $date => $company_array)
{
echo '<div class="Date">Bookings for '.$date.'</div>';
foreach( $company_array as $company => $student_array )
{
echo '<div class="Company">Bookings for '.$company.'</div>';
foreach( $student_array as $student )
{
echo '<div class="Student">'.$student.'</div>';
}
}
}
答案 1 :(得分:0)
使Call insertRows(tgtWorksheet, insertRow, NumRows)
成为多维数组
$bookings
新数组将返回:
$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" )
);
$arr = [];
foreach ($bookings as $row){
$arr[$row['Date']][$row['Company']][] = $row['Student'];
}
然后,您可以使用如下所示的3个循环进行数组处理。
Array
(
[12/05/2019] => Array
(
[Company 1] => Array
(
[0] => Student 1
[1] => Student 2
[2] => Student 3
[3] => Student 4
[4] => Student 5
)
)
)
答案 2 :(得分:0)
使用函数实现此功能的另一种有趣方式。...
$this->em->getRepository(...)