数组输出
print_r($hours_effort_result); //prints below output
Array
(
[0] => Array
(
)
[1] => Array
(
[0] => Array
(
[RESOURCE_ID] => 1
[EMPLOYEE_NAME] => User 1
[SITE_STATUS] => Onsite
[ACTUAL_HOURS] => 120
[MONTH_YEAR] => JUL-2015
)
[1] => Array
(
[RESOURCE_ID] => 2
[EMPLOYEE_NAME] => User 2
[SITE_STATUS] => Onsite
[ACTUAL_HOURS] => 140
[MONTH_YEAR] => JUL-2015
)
)
[2] => Array
(
[0] => Array
(
[RESOURCE_ID] => 3
[EMPLOYEE_NAME] => User 1
[SITE_STATUS] => Offshore
[ACTUAL_HOURS] => 170
[MONTH_YEAR] => AUG-2015
)
[1] => Array
(
[RESOURCE_ID] => 4
[EMPLOYEE_NAME] => User 2
[SITE_STATUS] => Offshore
[ACTUAL_HOURS] => 180
[MONTH_YEAR] => AUG-2015
)
)
[3] => Array
(
)
)
foreach ($hours_effort_result as $hours_effort_data){
echo '<pre>';print_r($hours_effort_data);//below output
}
Array
(
)
Array
(
[0] => Array
(
[RESOURCE_ID] => 1
[EMPLOYEE_NAME] => User 1
[SITE_STATUS] => Onsite
[ACTUAL_HOURS] => 120
[MONTH_YEAR] => JUL-2015
)
[1] => Array
(
[RESOURCE_ID] => 2
[EMPLOYEE_NAME] => User 2
[SITE_STATUS] => Onsite
[ACTUAL_HOURS] => 140
[MONTH_YEAR] => JUL-2015
)
)
Array
(
[0] => Array
(
[RESOURCE_ID] => 3
[EMPLOYEE_NAME] => User 1
[SITE_STATUS] => Offshore
[ACTUAL_HOURS] => 170
[MONTH_YEAR] => AUG-2015
)
[1] => Array
(
[RESOURCE_ID] => 4
[EMPLOYEE_NAME] => User 2
[SITE_STATUS] => Offshore
[ACTUAL_HOURS] => 180
[MONTH_YEAR] => AUG-2015
)
)
Array
(
)
显示月份和网站
print_R($project_months); //outputs below data
Array
(
[0] => JUN-2015
[1] => JUL-2015
[2] => AUG-2015
[3] => SEP-2015
)
echo '<tr>';
foreach($project_months as $month_year){
echo '<td>Site</strong></td>';
echo '<td>'.$month_year.'</td>';
}
echo '</tr>';
所需输出
Name of Engineer Site Jun-15 Site Jul-15 Site Aug-15 Site Sep-15
User 1 Onsite 120 Offshore 170
User 2 Onsite 140 Offshore 180
如您所见,月Jun
和Sep
不包含任何数据,因此数组为空,而其他月份包含以表格格式打印的数据。唯一要打印的数据是Engineer Name, Site and Hours
如何循环数组以实现上述输出?
答案 0 :(得分:1)
这假设你定义了这个变量:
$project_months = Array('JUN-2015', 'JUL-2015', 'AUG-2015', 'SEP-2015' );
首先,创建一个用户数组:
<?php
$user_array = Array();
foreach ( $project_months as $month )
{
foreach ($hours_effort_result as $key => $data)
{
if ( ! empty ($data) )
{
foreach ($data as $key => $val )
{
if ( $val['MONTH_YEAR'] == $month )
{
$user_array[$val['EMPLOYEE_NAME']][$month]['SITE_STATUS'] = $val['SITE_STATUS'];
$user_array[$val['EMPLOYEE_NAME']][$month]['ACTUAL_HOURS'] = $val['ACTUAL_HOURS'];
}
}
}
}
}
?>
其次,在表格中,循环浏览$user_array
并将n/a
添加到没有数据的那些月份。
<table>
<tr>
<th>Name</th>
<th>Site</th>
<th>Jun-15</th>
<th>Site</th>
<th>Jul-15</th>
<th>Site</th>
<th>Aug-15</th>
<th>Site</th>
<th>Sep-15</th>
</tr>
<?php foreach ($user_array as $user_name => $user_data): ?>
<tr>
<td><?php print ($user_name); ?></td>
<?php foreach ($project_months as $month): ?>
<?php if ( array_key_exists( $month, $user_data ) ): ?>
<td><?php print ($user_data[$month]['SITE_STATUS']); ?></td>
<td><?php print ($user_data[$month]['ACTUAL_HOURS']); ?></td>
<?php else: ?>
<td>n/a</td>
<td>n/a</td>
<?php endif; ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
答案 1 :(得分:0)
尝试在此代码中插入分隔线。
$table = '';
$table .= '<table>';
$table .= '<thead>';
$table .= '<th>Name of Engineer</th>';
$table .= '<th>Site</th>';
foreach ($arr as $key => $data)
{
foreach($data as $key2 => $val)
{
if ( ! empty($val) )
{
$table .= '<th>' . $val['MONTH_YEAR'] . '</th>';
}
else
{
$table .= '</th>';
}
}
}
$table .= '<br>';
$table .= '</thead>';
$table .= '<tbody>';
foreach ($arr as $key => $data)
{
foreach($data as $key2 => $val)
{
if ( ! empty($val) )
{
$table .= '<td>'.$val['EMPLOYEE_NAME'].'</td>';
$table .= '<td>'. $val['SITE_STATUS'] . '</td>';
$table .= '<td>'. $val['ACTUAL_HOURS'] .'</td>';
}
else
{
$table .= '<td></td>';
$table .= '<td></td>';
}
}
}
$table .= '</tbody>';
$table .= '</table>';
echo $table;