3v4l.org

run code in 300+ PHP versions simultaneously
<?php $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); ?> <table> <tr> <th>Name <?foreach($years as $y):?> <th colspan="<?=count($qs)?>"><?=$y?> <?endforeach?> <tr> <th>&nbsp; <?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] : '' ?></td> <?endforeach?> <?endforeach?> <?endforeach?> </table>
Output for 7.3.32 - 7.3.33, 7.4.33, 8.0.13, 8.5.1 - 8.5.3
<table> <tr> <th>Name <th colspan="3">2016 <th colspan="3">2017 <tr> <th>&nbsp; <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>
Output for 8.5.0
<table> <tr> <th>Name <?foreach($years as $y):?> <th colspan="3"> Warning: Undefined variable $y in /in/gGKeH on line 40 <?endforeach?> <tr> <th>&nbsp; <?foreach($years as $y):?> <?foreach($qs as $q):?> <th>q Warning: Undefined variable $q in /in/gGKeH on line 46 <?endforeach?> <?endforeach?> <?foreach($names as $name):?> <tr> <td> Warning: Undefined variable $name in /in/gGKeH on line 52 <?foreach($years as $y):?> <?foreach($qs as $q):?> <td> Warning: Undefined variable $name in /in/gGKeH on line 55 Deprecated: Using null as an array offset is deprecated, use an empty string instead in /in/gGKeH on line 55 Warning: Undefined variable $y in /in/gGKeH on line 55 Warning: Undefined variable $q in /in/gGKeH on line 55 </td> <?endforeach?> <?endforeach?> <?endforeach?> </table>
Output for 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.14, 8.4.16 - 8.4.17
<table> <tr> <th>Name <?foreach($years as $y):?> <th colspan="3"> Warning: Undefined variable $y in /in/gGKeH on line 40 <?endforeach?> <tr> <th>&nbsp; <?foreach($years as $y):?> <?foreach($qs as $q):?> <th>q Warning: Undefined variable $q in /in/gGKeH on line 46 <?endforeach?> <?endforeach?> <?foreach($names as $name):?> <tr> <td> Warning: Undefined variable $name in /in/gGKeH on line 52 <?foreach($years as $y):?> <?foreach($qs as $q):?> <td> Warning: Undefined variable $name in /in/gGKeH on line 55 Warning: Undefined variable $y in /in/gGKeH on line 55 Warning: Undefined variable $q in /in/gGKeH on line 55 </td> <?endforeach?> <?endforeach?> <?endforeach?> </table>
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.31, 7.4.0 - 7.4.32
<table> <tr> <th>Name <?foreach($years as $y):?> <th colspan="3"> Notice: Undefined variable: y in /in/gGKeH on line 40 <?endforeach?> <tr> <th>&nbsp; <?foreach($years as $y):?> <?foreach($qs as $q):?> <th>q Notice: Undefined variable: q in /in/gGKeH on line 46 <?endforeach?> <?endforeach?> <?foreach($names as $name):?> <tr> <td> Notice: Undefined variable: name in /in/gGKeH on line 52 <?foreach($years as $y):?> <?foreach($qs as $q):?> <td> Notice: Undefined variable: name in /in/gGKeH on line 55 Notice: Undefined variable: y in /in/gGKeH on line 55 Notice: Undefined variable: q in /in/gGKeH on line 55 </td> <?endforeach?> <?endforeach?> <?endforeach?> </table>

preferences:
219.16 ms | 417 KiB | 5 Q