Php 将复杂多维循环转换为表
我在从复杂循环填充表时遇到问题 该表由7天日历表组成,每天应包含其下的日期事件Php 将复杂多维循环转换为表,php,codeigniter,multidimensional-array,Php,Codeigniter,Multidimensional Array,我在从复杂循环填充表时遇到问题 该表由7天日历表组成,每天应包含其下的日期事件 Array ( [2012-12-16] => Array ( ) [2012-12-17] => Array ( [0] => Array ( [date] => 2012-12-17 [ti
Array
(
[2012-12-16] => Array
(
)
[2012-12-17] => Array
(
[0] => Array
(
[date] => 2012-12-17
[time_booked] => 00:09:00
[time_start] => 00:00:00
[time_end] => 00:00:00
[name] => maha mostafa elrashed
[first_name] => marwan
[title] => root cannal
)
[1] => Array
(
[date] => 2012-12-17
[time_booked] => 09:00:00
[time_start] => 00:00:00
[time_end] => 00:00:00
[name] => demo demo eldemo
[first_name] => marwan
[title] => ultrasound
)
)
[2012-12-18] => Array
(
[0] => Array
(
[date] => 2012-12-18
[time_booked] => 09:00:00
[time_start] => 00:00:00
[time_end] => 00:00:00
[name] => demo demo eldemo
[first_name] => marwan
[title] => root cannal
)
)
[2012-12-19] => Array
(
)
[2012-12-20] => Array
(
)
[2012-12-21] => Array
(
)
[2012-12-22] => Array
(
)
)
我想把这个数组的例子转换成一个表。目前我发现最简单的方法是将每一天打印为ul和float,但我真正想要的是用它们制作一个表,以便将来能够使用jQuery对该表进行排序
foreach($ztable as $t){
echo"\n<ul style='float:left;width:140px'>";
foreach($t as $r){
echo "<li class='{$r['first_name']}'>{$r['name']}</li>";
}
echo "</ul>\n";
}
foreach($t表为$t){
echo“\n”;
外汇($t作为$r){
echo“- {$r['name']}
”;
}
回声“
\n”;
}
有什么建议吗?
顺便说一下,我用的是Codeigniter 2
编辑:
上表是建议的答案,下表是我想要的答案。你的问题很模糊。您是说您想要构建一个
,同时拥有工作代码,而实际上您想要构建一个表。嗯
以下是如何创建表格:
// list of fields that will be rows
$fields = array(
'date' => 'Date',
'time_booked' => 'Time of booking',
'time_start' => 'Start time',
'time_end' => 'End time',
'name' => 'Name',
'first_name' => 'First name',
'title' => 'Title'
);
// echo table header
echo '<table border="1"><tr><th>Day</th>';
foreach ($fields as $field)
{
echo '<th>' . $field . '</th>';
}
echo '</tr>';
// echo table rows
foreach($ztable as $day => $data)
{
echo '<tr><td>' . $day . '</td>';
// if some field is not present for current day, we show '-'
foreach ($fields as $field_name => $field)
{
echo '<td>' . (isset($data[$field_name]) ? $data[$field_name] : '-') . '</td>';
}
echo '</tr>';
}
echo '</table>';
//将成为行的字段列表
$fields=数组(
“日期”=>“日期”,
“预订时间”=>“预订时间”,
“开始时间”=>“开始时间”,
“结束时间”=>“结束时间”,
“名称”=>“名称”,
“名字”=>“名字”,
“标题”=>“标题”
);
//回音表标题
回声“天”;
foreach($fields作为$field)
{
回显“.$field.”;
}
回声';
//回显表行
foreach($ztable as$day=>$data)
{
回声“.$day.”;
//如果某个字段当前不存在,则显示“-”
foreach($fields作为$field\u name=>$field)
{
回显“”。(isset($data[$field_name])?$data[$field_name]:“-”)。”;
}
回声';
}
回声';
我认为您的数组格式不正确。请尝试这种格式
Array('2012-12-16' => Array(),
'2012-12-17' => Array
(
'0' => Array
(
'date' => '2012-12-17',
'time_booked' => '00:09:00',
'time_start' => '00:00:00',
'time_end' => '00:00:00',
'name' => 'maha mostafa elrashed',
'first_name' => 'marwan',
'title' => 'root cannal'
),
'1' => Array
(
'date' => '2012-12-17',
'time_booked' => '09:00:00',
'time_start' => '00:00:00',
'time_end' => '00:00:00',
'name' => 'demo demo eldemo',
'first_name' => 'marwan',
'title' => 'ultrasound'
)
),
'2012-12-18' => Array
(
'0' => Array
(
'date' => '2012-12-18',
'time_booked' => '09:00:00',
'time_start' => '00:00:00',
'time_end' => '00:00:00',
'name' => 'demo demo eldemo',
'first_name' => 'marwan',
'title' => 'root cannal'
)
),
'2012 - 12 - 19' => Array
(
),
'2012 - 12 - 20' => Array
(
),
'2012 - 12 - 21' => Array
(
),
'2012 - 12 - 22' => Array
(
)
);
由于您添加了一张包含所需内容的图片,因此我收集了一些外观粗糙的代码,以获得结果:
//provided $a is the array
//table header with dates
echo "<table class='table table-bordered'>";
echo "<tr>";
foreach($a as $date => $data){
echo "<th>$date</th>";
}
echo "</tr>";
//table row for appointment dates
echo "<tr>";
foreach($a as $k=>$v){
//if the array value isn't empty, we have data
if(! empty($v)){
echo "<td>";
echo "<ul>";
//loop through the array to get all the names
foreach($v as $k => $v){
echo "<li>".$v['name']."</li>";
}
echo "</ul>";
echo "</td>";
}else{
//there was no data for the date; empty td
echo "<td></td>";
}
}
echo "</tr></table>";
//提供的$a是数组
//带日期的表格标题
回声“;
回声“;
foreach($a作为$date=>$data){
回显“$date”;
}
回声“;
//用于约会日期的表行
回声“;
foreach($a为$k=>$v){
//如果数组值不是空的,我们就有数据
如果(!空($v)){
回声“;
回声“”;
//循环遍历数组以获取所有名称
foreach($v为$k=>$v){
回声“- ”$v['name']。“
- ”;
}
回声“
”;
回声“;
}否则{
//没有日期的数据;为空
回声“;
}
}
回声“;
应该足够让你走上正轨了 我认为这不会完全奏效;需要遍历
$data
,因为它将是一个数组如果(!empty($data)){foreach($k=>$v的数据){echo$v[$field_name]。“”;}}或者提供了{echo“-”;}`@根据数组判断,他可能在几天内没有数据,我假设在某些情况下会有不完整的数据集。这就是为什么在
$data
中循环可能破坏表布局,而在预定义的字段集中循环将始终生成相同的布局,其中包含数据或没有数据的带“-”的布局。在我注释的循环中,它首先检查数组是否为空,是否在其中放置“-”如果(!empty($data))嗯,yp我希望它出现在一个表中,但我希望日期能够在一天中包含多行(行将只显示[名字]),我很确定您在OP中看到的是数组的转储,而不是数组的设置方式。@stormdrain yp是数组的转储查看CI自动表生成: