我有一个简单的问题。我一直试图在此代码中显示类别编号(或此处的列号)。
$cat[1] = ["A"];
$cat[2] = ["B"];
while($row = $result->fetch_assoc()) {
$columns[1] = array_filter($row, function($col) use ($cat) {
if(in_array($col, $cat[1])) {
return true;
}
});
$columns[2] = array_filter($row, function($col) use ($cat) {
if(in_array($col, $cat[2])) {
return true;
}
});
$output[] = $columns;
}
$table = <<<EOT
<table>
<tr>
<th>R1</th>
<th>R2</th>
</tr>
EOT;
foreach($output as $row) {
$table.="<tr>";
foreach($row as $column) {
$table .="<td>";
$values = // count($column); // this is where I need help.
$table .= $values;
$table .="</td>";
}
$table.="</tr>";
}
$table.="</table>";
echo $table;
我在这里需要帮助:$values = count($column);
我想不通正确的功能。如果不为空,我需要显示类别(列)号,而不是计算类别的内容。
答案 0 :(得分:1)
我认为您在代码中所需要做的就是获取数组元素的键。
foreach ($output as $row) {
$table .= "<tr>";
// Add $key => here VVV
foreach ($row as $key => $column) {
$table .= "<td>";
$values = $key; // Because you only have 2 keys in your array this should be 1 or 2
$table .= $values;
$table .= "</td>";
}
$table .= "</tr>";
}