简体   繁体   中英

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. For example:

Jaar | Basis | Secundair

2021 | Wiskunde | PAV 3de graad b-stroom
2021 | --------------| Burgerzin 3de graad b-stroom
2020 | Informatieverwerving en -verwerking met ICT | Kritisch 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:

$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($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];

Don't forget to add <tbody> tags at the beginning and the the end:

$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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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