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; } $test_obj[$obj_added]["y"] = (int) $year; $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 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 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
Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /in/2Ldej:37 Stack trace: #0 {main} thrown in /in/2Ldej on line 37
Process exited with code 255.
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 Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /in/2Ldej:37 Stack trace: #0 {main} thrown in /in/2Ldej on line 37
Process exited with code 255.

preferences:
203.92 ms | 402 KiB | 160 Q