我正在尝试在关联数组上使用foreach循环创建html表。这里是有关数组的更多详细信息。
<?php
$assoc_array = array(
"0" => array(
"project_id" => "1",
"emp_id" => "123",
"emp_name" => "Max",
"project" => "Java"
),
"1" => array(
"project_id" => "2",
"emp_id" => "123",
"emp_name" => "Max",
"project" => "Java"
),
"2" => array(
"project_id" => "3",
"emp_id" => "124",
"emp_name" => "Max",
"project" => "Java"
),
"3" => array(
"project_id" => "4",
"emp_id" => "124",
"emp_name" => "Max",
"project" => "Java"
),
"4" => array(
"project_id" => "5",
"emp_id" => "125",
"emp_name" => "Max",
"project" => "Java"
),
"5" => array(
"project_id" => "6",
"emp_id" => "125",
"emp_name" => "Max",
"project" => "Java"
)
)
?>
所以我试图将它们与他们的emp_id分组。 为此,我正在尝试使用以下逻辑
<?php
$assoc_array = array(
"0" => array(
"project_id" => "1",
"emp_id" => "123",
"emp_name" => "Max",
"project" => "Java"
),
"1" => array(
"project_id" => "2",
"emp_id" => "123",
"emp_name" => "Max",
"project" => "Java"
),
"2" => array(
"project_id" => "3",
"emp_id" => "124",
"emp_name" => "Anna",
"project" => "Angular"
),
"3" => array(
"project_id" => "4",
"emp_id" => "124",
"emp_name" => "Anna",
"project" => "PHP"
),
"4" => array(
"project_id" => "5",
"emp_id" => "125",
"emp_name" => "John",
"project" => "Java"
),
"5" => array(
"project_id" => "6",
"emp_id" => "125",
"emp_name" => "John",
"project" => "Java"
)
);
$prevEmpid = '';
$tableData = '';
$tableStartTag = "<table>";
$tableEndTag = "</table>";
$wholeData = "";
$trFixed = "<tr><td>project id </td><td>emp id </td><td>emp name</td><td>project</td></tr>";
foreach($assoc_array as $val) {
if($val['emp_id'] !== $prevEmpid ) {
if($tableData) {
$wholeData = $tableStartTag . $trFixed . $tableData . $tableEndTag;
echo $tableData;
}
$wholeData = '';
$tableData = '';
$prevEmpid = $val['emp_id'];
}
$tableData .= "<tr><td>" . $val['project_id'] . "</td>"
. "<td>" . $val['emp_id'] . "<td>"
. "<td>" . $val['emp_name'] . "<td>"
. "<td>" . $val['project'] . "<td></tr>";
}
但是问题是,因为我没有得到最后一个表数据,因为foreach循环中的元素经历了else条件。
任何小小的帮助都非常感谢。
答案 0 :(得分:1)
你说
但是问题是,自从我没有得到最后的表数据以来, foreach循环中的元素经历else条件
这是因为这里的这段代码将$prevEmpid
设置为当前迭代之一。
if($val['emp_id'] !== $prevEmpid ) {
if($tableData) {
echo $tableData;
}
$tableData = '';
$prevEmpid = $val['emp_id'];
} else {
$tableData .= 'some code to create table with data ';
}
最后一项不会显示,因为它们的emp_id
与您的代码所指定的完全相同。
"4" => array(
"project_id" => "5",
"emp_id" => "125",
"emp_name" => "John",
"project" => "Java"
),
"5" => array(
"project_id" => "6",
"emp_id" => "125",
"emp_name" => "John",
"project" => "Java"
)
答案 1 :(得分:1)
您丢失了一些项目,因为仅在id更改时才添加输出数据($ tableData。=)。完全删除其他运算符。并在循环后添加echo以输出最后一组。
但是,您可以通过简单的代码对数组进行分组
$new = [];
foreach($assoc_array as $val) {
$new[$val['emp_id']][] = $val;
}
然后通过另一个循环输出每个组
答案 2 :(得分:1)
我假设您希望多个表按emp_id分组。
解决方案是,首先需要将 emp_id 划分为数组组。之后,使用for循环创建多个表。
<?php
$projects = [
[
'project_id' => '1',
'emp_id' => '123',
'emp_name' => 'Max',
'project' => 'Java'
],
[
'project_id' => '2',
'emp_id' => '123',
'emp_name' => 'Max',
'project' => 'Java'
],
[
'project_id' => '3',
'emp_id' => '124',
'emp_name' => 'Max',
'project' => 'Java'
],
[
'project_id' => '4',
'emp_id' => '124',
'emp_name' => 'Max',
'project' => 'Java'
],
[
'project_id' => '5',
'emp_id' => '125',
'emp_name' => 'Max',
'project' => 'Java'
],
[
'project_id' => '6',
'emp_id' => '125',
'emp_name' => 'Max',
'project' => 'Java'
]
];
$empGroups = [];
foreach ($projects as $project) {
$empGroups[$project['emp_id']][] = $project;
}
?>
<?php foreach ($empGroups as $empGroup) { ?>
<table>
<tr>
<td>project_id</td>
<td>emp_id</td>
<td>emp_name</td>
<td>project</td>
</tr>
<?php foreach ($empGroup as $pjs) { ?>
<tr>
<?php foreach ($pjs as $pj) { ?>
<td>
<?php echo $pj?>
</td>
<?php } ?>
</tr>
<?php } ?>
</table>
<?php } ?>
答案 3 :(得分:1)
<?php
// given following array:
$assoc_array = array(
"0" => array(
"project_id" => "1",
"emp_id" => "123",
"emp_name" => "Max",
"project" => "Java"
),
"1" => array(
"project_id" => "2",
"emp_id" => "123",
"emp_name" => "Max",
"project" => "Java"
),
"2" => array(
"project_id" => "3",
"emp_id" => "124",
"emp_name" => "Anna",
"project" => "Angular"
),
"3" => array(
"project_id" => "4",
"emp_id" => "124",
"emp_name" => "Anna",
"project" => "PHP"
),
"4" => array(
"project_id" => "5",
"emp_id" => "125",
"emp_name" => "John",
"project" => "Java"
),
"5" => array(
"project_id" => "6",
"emp_id" => "125",
"emp_name" => "John",
"project" => "Java"
)
);
// you can create function to display table groupings
function print_table($arr){
$prevEmpid = '';
$tableData = '';
$header = "<tr>
<td>project_id</td><td>emp_id</td><td>emp_name</td><td>project</td>
</tr>";
$temp = '';
echo '<table>',"\n";
foreach($arr as $val) {
// comparing current value of $val['emp_id'] with $temp
if ($val['emp_id'] != $temp) {
$tableData .= $header;
}
$tableData .= "\n<tr>\n\t<td>".$val['project_id']."</td>";
$tableData .= '<td>'.$val['emp_id']."</td>";
$tableData .= '<td>'.$val['emp_name']."</td>";
$tableData .= '<td>'.$val['project']."</td>\n</tr>\n";
// save current value of $val['emp_id'] in temp
$temp = $val['emp_id'];
}
echo $tableData,"</table>\n";
}
print_table($assoc_array);
查看实时代码here
答案 4 :(得分:0)
这是我为最后一行实现逻辑以完成html表格式的方式。
$prevEmpid = '';
$tableData = '';
$tableStartTag = "<table>";
$tableEndTag = "</table>";
$wholeData = "";
$trFixed = "<tr><td>project id </td><td>emp id </td><td>emp name</td><td>project</td></tr>";
foreach($assoc_array as $val) {
if($val['emp_id'] !== $prevEmpid ) {
if($tableData) {
$wholeData = $tableStartTag . $trFixed . $tableData . $tableEndTag;
echo $tableData;
}
$wholeData = '';
$tableData = '';
$prevEmpid = $val['emp_id'];
}
$tableData .= "<tr><td>" . $val['project_id'] . "</td>"
. "<td>" . $val['emp_id'] . "<td>"
. "<td>" . $val['emp_name'] . "<td>"
. "<td>" . $val['project'] . "<td></tr>";
}
if($tableData) {
$wholeData = $tableStartTag . $trFixed . $tableData . $tableEndTag;
echo $tableData;
}