3v4l.org

run code in 300+ PHP versions simultaneously
<?php $tests = array("HIV", "Chlamydia", "Gonorrhoea"); $years = [ '2011' => false, '2012' => false, '2013' => false, '2014' => false, '2015' => false, '2016' => false, '2017' => false ]; $this_year = date('Y'); if (!array_key_exists($this_year, $years)) { // add to the array: $years[$this_year] = false; } $tests_by_year = []; $test_obj = []; $obj_count = count($years); $obj_added = 0; foreach ( $tests as $value ) { foreach ($years as $year => $yval) { $tests_by_year[$value][$year] = 0; if ($obj_added <= $obj_count) { $test_obj[] = new \stdClass; } $obj_added++; } } var_dump($test_obj); $test_obj2 = [ 0 => new \stdClass, 1 => new \stdClass, 2 => new \stdClass, 3 => new \stdClass, 4 => new \stdClass, 5 => new \stdClass, 6 => new \stdClass, 7 => new \stdClass, 8 => new \stdClass, 9 => new \stdClass ]; $test_obj2[0]->y = (int)2011; $test_obj2[1]->y = (int)2012; $test_obj2[2]->y = (int)2013; $test_obj2[3]->y = (int)2014; $test_obj2[4]->y = (int)2015; $test_obj2[5]->y = (int)2016; $test_obj2[6]->y = (int)2017; $test_obj2[7]->y = (int)2018; $test_obj2[8]->y = (int)2019; $test_obj2[9]->y = (int)2020; var_dump($test_obj2); /* foreach ($tests_by_year as $key => $value) { $test_obj[0]->$key = $value['2011']; $test_obj[1]->$key = $value['2012']; $test_obj[2]->$key = $value['2013']; $test_obj[3]->$key = $value['2014']; $test_obj[4]->$key = $value['2015']; $test_obj[5]->$key = $value['2016']; $test_obj[6]->$key = $value['2017']; $test_obj[7]->$key = $value['2018']; $test_obj[8]->$key = $value['2019']; $test_obj[9]->$key = $value['2020']; }*/
Output for 5.6.38, 7.0.33, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
array(9) { [0]=> object(stdClass)#1 (0) { } [1]=> object(stdClass)#2 (0) { } [2]=> object(stdClass)#3 (0) { } [3]=> object(stdClass)#4 (0) { } [4]=> object(stdClass)#5 (0) { } [5]=> object(stdClass)#6 (0) { } [6]=> object(stdClass)#7 (0) { } [7]=> object(stdClass)#8 (0) { } [8]=> object(stdClass)#9 (0) { } } array(10) { [0]=> object(stdClass)#10 (1) { ["y"]=> int(2011) } [1]=> object(stdClass)#11 (1) { ["y"]=> int(2012) } [2]=> object(stdClass)#12 (1) { ["y"]=> int(2013) } [3]=> object(stdClass)#13 (1) { ["y"]=> int(2014) } [4]=> object(stdClass)#14 (1) { ["y"]=> int(2015) } [5]=> object(stdClass)#15 (1) { ["y"]=> int(2016) } [6]=> object(stdClass)#16 (1) { ["y"]=> int(2017) } [7]=> object(stdClass)#17 (1) { ["y"]=> int(2018) } [8]=> object(stdClass)#18 (1) { ["y"]=> int(2019) } [9]=> object(stdClass)#19 (1) { ["y"]=> int(2020) } }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 array(9) { [0]=> object(stdClass)#1 (0) { } [1]=> object(stdClass)#2 (0) { } [2]=> object(stdClass)#3 (0) { } [3]=> object(stdClass)#4 (0) { } [4]=> object(stdClass)#5 (0) { } [5]=> object(stdClass)#6 (0) { } [6]=> object(stdClass)#7 (0) { } [7]=> object(stdClass)#8 (0) { } [8]=> object(stdClass)#9 (0) { } } array(10) { [0]=> object(stdClass)#10 (1) { ["y"]=> int(2011) } [1]=> object(stdClass)#11 (1) { ["y"]=> int(2012) } [2]=> object(stdClass)#12 (1) { ["y"]=> int(2013) } [3]=> object(stdClass)#13 (1) { ["y"]=> int(2014) } [4]=> object(stdClass)#14 (1) { ["y"]=> int(2015) } [5]=> object(stdClass)#15 (1) { ["y"]=> int(2016) } [6]=> object(stdClass)#16 (1) { ["y"]=> int(2017) } [7]=> object(stdClass)#17 (1) { ["y"]=> int(2018) } [8]=> object(stdClass)#18 (1) { ["y"]=> int(2019) } [9]=> object(stdClass)#19 (1) { ["y"]=> int(2020) } }

preferences:
196.24 ms | 404 KiB | 179 Q