<?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'];
}*/
preferences:
54.73 ms | 402 KiB | 5 Q