简体   繁体   English

用两个多维数组构建 HTML 表格

[英]Building of HTML table with two multidimensional arrays

I have inside one array two arrays with keys and I want to output this in HTML table by year date.我在一个数组中有两个带键的数组,我想按年份在 HTML 表中输出它。 For example:例如:

Jaar |贾尔 | Basis |基础 | Secundair第二代

2021 | 2021 | Wiskunde |维斯昆德 | PAV 3de graad b-stroom PAV 3de graad b-stroom
2021 | 2021 | --------------| --------------| Burgerzin 3de graad b-stroom Burgerzin 3de graad b-stroom
2020 | 2020 | Informatieverwerving en -verwerking met ICT | Informatieeverwerving en -verwerking 遇到了 ICT | Kritisch denken & mediawijsheid Kritsch denken & mediawijsheid

My input arrays are like this:我的输入数组是这样的:

array(2) {
  [0]=>
  array(1) {
    [0]=>
    array(2) {
      ["jaar"]=>
      string(4) "2021"
      ["basis"]=>
      string(8) "Wiskunde"
    }
  }
  [1]=>
  array(2) {
    [0]=>
    array(3) {
      ["jaar_so"]=>
      string(4) "2021"
      ["sec"]=>
      string(3) "PAV"
      ["onderwijsniveau"]=>
      string(9) "3de graad"
    }
    [1]=>
    array(3) {
      ["jaar_so"]=>
      string(4) "2021"
      ["sec"]=>
      string(9) "Burgerzin"
      ["onderwijsniveau"]=>
      string(18) "3de graad B-stroom"
    }
  }
}
array(2) {
  [0]=>
  array(1) {
    [0]=>
    array(2) {
      ["jaar"]=>
      string(4) "2020"
      ["basis"]=>
      string(43) "Informatieverwerving en -verwerking met ICT"
    }
  }
  [1]=>
  array(1) {
    [0]=>
    array(3) {
      ["jaar_so"]=>
      string(4) "2020"
      ["sec"]=>
      string(35) "Kritisch denken & mediawijsheid"
      ["onderwijsniveau"]=>
      string(9) "3e graad "
    }
  }
}
}

I've tried with this foreach loops:我试过这个 foreach 循环:

$output = "<table class='noborder homepeilingskalender fixed_header'><thead><tr><th>"
.get_field('titel_eerste_kolom')."</th><th>"
.get_field('titel_tweede_kolom')."</th><th>"
.get_field('titel_derde_kolom')."</th></tr></thead>";

foreach ($combined as $key2 => $array_keys) {
     foreach ($array_keys as $key => $value) {
         foreach($value as $key3 =>$value_def){

             $output .= "<tr><td>".$value_def['jaar']
                           ."</td><td>$value_def["basis"]
                           ."</td><td>".$value_def["sec"]
                           ."</td></tr>";    
         }
     }
 }
$output .= "</table>";

You can use next foreach loop:您可以使用下一个foreach循环:

foreach($all_ars as $ar){
    $mark = 0;
    foreach($ar[0] as $fkey => $fval){
        foreach($ar[1] as $skey => $sval){ 
            $fval['basis'] = !$mark ? $fval['basis'] : '';
            $output .= "<tr><td>".$fval['jaar']."</td><td>".$fval['basis']. "</td><td>".$sval['sec']."</td></tr>";
            if ($fval['basis']) $mark = 1;
        }
    } 
} 

where $all_ars = [$ar1,$ar2]; $all_ars = [$ar1,$ar2];

Don't forget to add <tbody> tags at the beginning and the the end:不要忘记在开头和结尾添加<tbody>标签:

$output = "<table class='noborder homepeilingskalender fixed_header'><thead><tr><th>".get_field('titel_eerste_kolom')."</th><th>".get_field('titel_tweede_kolom')."</th><th>".get_field('titel_derde_kolom')."</th></tr></thead><tbody>";

and

$output .= "</tbody></table>";  

Also be careful with using ' and " inside loop concatenation, you need to use one of this.还要小心使用'"内部循环连接,您需要使用其中之一。

Demo演示

Your code misses some quotation marks in the line where you append to $output .您的代码在附加到$output的行中遗漏了一些引号。

Furthermore your array-structure from the output does not seem to fit the way you treat it in your code.此外,您输出的数组结构似乎不适合您在代码中处理它的方式。 I think you should go over your code again and make sure everything fits together.我认为您应该再次检查您的代码并确保所有内容都适合。 Think of the "outer" array as the rows of your table and the the field of the "inner" arrays as the cells to the respective row.将“外部”数组视为表格的行,将“内部”数组的字段视为相应行的单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM