3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = '2020-01; Starter1; 11,4% 2020-01; Starter2; 6,9% 2020-01; Starter3; 9,5% 2020-01; Starter4; 11,1% 2020-02; Starter1; 5,8% 2020-02; Starter2; 5,8% 2020-02; Starter3; 10,0% 2020-02; Starter4; 6,4%'; // normalise and group data $result = []; foreach(str_getcsv($str, "\n") as $row){ $row = array_map('trim', str_getcsv($row, ";")); $result[$row[0]][$row[1]] = $row[2]; } // loop over to specification echo 'Year; Starter1; Starter2; Starter3; Starter4'.PHP_EOL; foreach ($result as $year => $row) { echo $year.'; '; foreach ($row as $key => $col) echo $col.";".str_repeat(' ', (($v = strlen($key)-(strlen($col)-1)) && $v > 0) ? $v : 0); echo PHP_EOL; }
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.26, 8.0.0 - 8.0.13, 8.1rc1 - rc3
Year; Starter1; Starter2; Starter3; Starter4 2020-01; 11,4%; 6,9%; 9,5%; 11,1%; 2020-02; 5,8%; 5,8%; 10,0%; 6,4%;

preferences:
42.66 ms | 460 KiB | 5 Q