これを取得するには(あなたが望むものに最も近い有効なJSON):
{"success":true, "years":[2012,2013,...]}
以下を使用できます。
$rt = array();
$rt["success"] = true;
$rt["years"] = array();
for ($i=intval(date('Y')) ; $i < (date('Y')+21) ; $i++) {
$rt['years'][]= $i;
}
echo json_encode($rt);
//{"success":true,"years":[2012,2013,2014,2015,2016,2017,2018,2019,2020,2021,2022,2023,2024,2025,2026,2027,2028,2029,2030,2031,2032]}
為に"years": [{"yearnumber": 2012}, {"yearnumber": 2013}]
以下を使用できます。
$rt = array();
$rt["success"] = true;
$rt["years"] = array();
for ($i=intval(date('Y')) ; $i < (date('Y')+21) ; $i++) {
$rt['years'][]= array("yearnumber" => $i);
}
echo json_encode($rt);
//{"success":true,"years":[{"yearnumber":2012},{"yearnumber":2013},{"yearnumber":2014},{"yearnumber":2015},{"yearnumber":2016},{"yearnumber":2017},{"yearnumber":2018},{"yearnumber":2019},{"yearnumber":2020},{"yearnumber":2021},{"yearnumber":2022},{"yearnumber":2023},{"yearnumber":2024},{"yearnumber":2025},{"yearnumber":2026},{"yearnumber":2027},{"yearnumber":2028},{"yearnumber":2029},{"yearnumber":2030},{"yearnumber":2031},{"yearnumber":2032}]}
私には冗長に見えますが