3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ "Jan 2016" => 2, "Feb 2016" => 4, "Mar 2016" => 2, "Apr 2016" => 0, "Jan 2017" => 9, "Feb 2017" => 2, "Mar 2017" => 5, "Jan 2018" => 4, "Feb 2018" => 6 ]; foreach ($array as $date => &$amount) { $year = substr($date, -4); $amount = ['month' => $amount, 'total' => &$ref[$year]]; $ref[$year] = ($ref[$year] ?? 0) + $amount['month']; } var_export($array);
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.4.16, 8.5.0 - 8.5.1
array ( 'Jan 2016' => array ( 'month' => 2, 'total' => 8, ), 'Feb 2016' => array ( 'month' => 4, 'total' => 8, ), 'Mar 2016' => array ( 'month' => 2, 'total' => 8, ), 'Apr 2016' => array ( 'month' => 0, 'total' => 8, ), 'Jan 2017' => array ( 'month' => 9, 'total' => 16, ), 'Feb 2017' => array ( 'month' => 2, 'total' => 16, ), 'Mar 2017' => array ( 'month' => 5, 'total' => 16, ), 'Jan 2018' => array ( 'month' => 4, 'total' => 10, ), 'Feb 2018' => array ( 'month' => 6, 'total' => 10, ), )
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.

preferences:
150.29 ms | 408 KiB | 5 Q