Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 显示类别编号(或列#)_Php - Fatal编程技术网

Php 显示类别编号(或列#)

Php 显示类别编号(或列#),php,Php,我有一个简单的问题。我一直试图在这段代码中显示类别号(或列号,在这里是相同的) $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

我有一个简单的问题。我一直试图在这段代码中显示类别号(或列号,在这里是相同的)

$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;
$cat[1]=[“A”];
$cat[2]=“B”];
而($row=$result->fetch_assoc()){
$columns[1]=数组过滤器($row,function($col)use($cat){
if(在数组中($col,$cat[1]){
返回true;
}
});
$columns[2]=数组过滤器($row,function($col)use($cat){
if(在数组中($col,$cat[2]){
返回true;
}
});
$output[]=$columns;
}

$table=我认为代码中需要的只是获取数组元素的键

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>";
}
foreach($output as$row){
$table.=”;
//在此处添加$key=>VVV
foreach($key=>$column的行){
$table.=”;
$values=$key;//因为数组中只有2个键,所以应该是1或2
$table.=$value;
$table.=”;
}
$table.=”;
}

在示例代码中,为了简单起见,我只包含了两列/类别。这适用于更多类别吗?因为我有更多的实际数据。谢谢。我试过了,得到了以下错误:(!)警告:为..中的foreach()提供的参数无效。。。。。第220行。这是
foreach($row as$key=>$column){
您的代码示例不太清楚列是什么以及为什么要这样筛选它们。只要您填充键,它就应该工作,例如
$columns[2]
当您创建列时。谢谢,达曼。它可以工作。即使类别为空,此代码也会显示$key?在原始代码中,我使用count,如果为空,则显示为零。我认为您应该更清楚地解释您的完整要求。不能有空键,可以是零,但不能为空。