To begin with, we will regroup the data taking into account dependencies.
At the same time, let's save the names / years / qs sets for building the header.
$rows = [ ['name' => 'name_one', 'y' => '2016', 'q' => '2', 'st' => 1], ['name' => 'name_one', 'y' => '2017', 'q' => '3', 'st' => 2], ['name' => 'name_two', 'y' => '2017', 'q' => '1', 'st' => 1], ['name' => 'name_two', 'y' => '2017', 'q' => '2', 'st' => 2], ]; $data = []; $names = []; $years = []; $qs = []; foreach ($rows as $row) { $link = &$data; foreach (['name', 'y'] as $field) { if (!isset($link[$row[$field]])) $link[$row[$field]] = []; $link = &$link[$row[$field]]; } $link[$row['q']] = $row['st']; unset($link); // $names[$row['name']] = true; $years[$row['y']] = true; $qs[$row['q']] = true; } // Опционально сортируем $names = array_keys($names); sort($names); $years = array_keys($years); sort($years); $qs = array_keys($qs); sort($qs);
The structure is there, the matter is small - to render the table: In the header there are 2 lines, in the first years there is a corresponding colspan .
Next, display the data itself.
foreach($years as $y => $_) can be used to iterate over the keys, however, we have already extracted the keys using array_keys .
Feeddle
?> <table border="1"> <tr> <th>Name <?foreach($years as $y):?> <th colspan="<?=count($qs)?>"><?=$y?> <?endforeach?> <tr> <th> <?foreach($years as $y):?> <?foreach($qs as $q):?> <th>q<?=$q?> <?endforeach?> <?endforeach?> <?foreach($names as $name):?> <tr> <td><?=$name?> <?foreach($years as $y):?> <?foreach($qs as $q):?> <td><?=isset($data[$name][$y][$q]) ? $data[$name][$y][$q] : '' ?> <?endforeach?> <?endforeach?> <?endforeach?> </table>
The result will look something like this:
<table border=1> <tr> <th>Name <th colspan="3">2016 <th colspan="3">2017 <tr> <th> <th>q1 <th>q2 <th>q3 <th>q1 <th>q2 <th>q3 <tr> <td>name_one <td></td> <td>1</td> <td></td> <td></td> <td></td> <td>2</td> <tr> <td>name_two <td></td> <td></td> <td></td> <td>1</td> <td>2</td> <td></td> </table>
код- vp_arth[name_one => [2016 => [q1 => null, q2 => 1, ..], ..], ..]before rendering - vp_arth