Carbonで1年分の月添字日付連想配列を作る

## 概要

月を添え字にした1年分の日付配列を作る機会があったので忘備録

こういうのを作る
```:日付の文字列の所には実際はCarbonオブジェクトが入る
'1' => [
    0 => "2022-01-01",
    1 => "2022-01-02",
    2 => "2022-01-03",
    3 => "2022-01-04",
    4 => "2022-01-05",
    5 => "2022-01-06",
    6 => "2022-01-07",
    7 => "2022-01-08",
    8 => "2022-01-09",
    9 => "2022-01-10",
    10 => "2022-01-11",
 ],
'2' => [
    0 => "2022-02-01",
    1 => "2022-02-02",
    2 => "2022-02-03",
]
```

<details><summary>実物(かなり長い)</summary>
<div>

```php
array:12 [▼
  1 => array:31 [▶
    0 => Carbon\Carbon @1640962800 {#1550 ▶
      #endOfTime: false
      #startOfTime: false
      #constructedObjectId: "000000000000060e0000000000000000"
      #localMonthsOverflow: null
      #localYearsOverflow: null
      #localStrictModeEnabled: null
      #localHumanDiffOptions: null
      #localToStringFormat: null
      #localSerializer: null
      #localMacros: null
      #localGenericMacros: null
      #localFormatFunction: null
      #localTranslator: null
      #dumpProperties: array:3 [ …3]
      #dumpLocale: null
      #dumpDateProperties: null
      date: 2022-01-01 00:00:00.0 Asia/Tokyo (+09:00)
    }
    1 => Carbon\Carbon @1641049200 {#1551 ▶
      #endOfTime: false
      #startOfTime: false
      #constructedObjectId: "000000000000060f0000000000000000"
      #localMonthsOverflow: null
      #localYearsOverflow: null
      #localStrictModeEnabled: null
      #localHumanDiffOptions: null
      #localToStringFormat: null
      #localSerializer: null
      #localMacros: null
      #localGenericMacros: null
      #localFormatFunction: null
      #localTranslator: null
      #dumpProperties: array:3 [ …3]
      #dumpLocale: null
      #dumpDateProperties: null
      date: 2022-01-02 00:00:00.0 Asia/Tokyo (+09:00)
    }
    2 => Carbon\Carbon @1641135600 {#1552 ▶
      #endOfTime: false
      #startOfTime: false
      #constructedObjectId: "00000000000006100000000000000000"
      #localMonthsOverflow: null
      #localYearsOverflow: null
      #localStrictModeEnabled: null
      #localHumanDiffOptions: null
      #localToStringFormat: null
      #localSerializer: null
      #localMacros: null
      #localGenericMacros: null
      #localFormatFunction: null
      #localTranslator: null
      #dumpProperties: array:3 [ …3]
      #dumpLocale: null
      #dumpDateProperties: null
      date: 2022-01-03 00:00:00.0 Asia/Tokyo (+09:00)
    }
    3 => Carbon\Carbon @1641222000 {#1553 ▶
      #endOfTime: false
      #startOfTime: false
      #constructedObjectId: "00000000000006110000000000000000"
      #localMonthsOverflow: null
      #localYearsOverflow: null
      #localStrictModeEnabled: null
      #localHumanDiffOptions: null
      #localToStringFormat: null
      #localSerializer: null
      #localMacros: null
      #localGenericMacros: null
      #localFormatFunction: null
      #localTranslator: null
      #dumpProperties: array:3 [ …3]
      #dumpLocale: null
      #dumpDateProperties: null
      date: 2022-01-04 00:00:00.0 Asia/Tokyo (+09:00)
    }
    4 => Carbon\Carbon @1641308400 {#1554 ▶
      #endOfTime: false
      #startOfTime: false
      #constructedObjectId: "00000000000006120000000000000000"
      #localMonthsOverflow: null
      #localYearsOverflow: null
      #localStrictModeEnabled: null
      #localHumanDiffOptions: null
      #localToStringFormat: null
      #localSerializer: null
      #localMacros: null
      #localGenericMacros: null
      #localFormatFunction: null
      #localTranslator: null
      #dumpProperties: array:3 [ …3]
      #dumpLocale: null
      #dumpDateProperties: null
      date: 2022-01-05 00:00:00.0 Asia/Tokyo (+09:00)
    }

```
　
....以下12月の31日まで
</div>
</details>

## コード
```php:こーど
$baseCarbon = new Carbon();
$baseCarbon->year = 2022;

$firstMonthCarbon = $baseCarbon->copy();
$firstMonthCarbon->month = 1;

$endMonthCarbon = $baseCarbon->copy();
$endMonthCarbon->month = 12;

//1月から12月までの最初の日付を格納した配列
// 2022-01-01,2022-02-01...
$oneYearFirstOfMonths = CarbonPeriod::create($firstMonthCarbon->firstOfMonth(),
    $endMonthCarbon->lastOfMonth())->months()->toArray();

$monthGroupDays = [];
foreach ($oneYearFirstOfMonths as $monthStartDay) {
    //月が添え字の月初〜月末の配列を作る
    $monthGroupDays[(string) $monthStartDay->month] = CarbonPeriod::create($monthStartDay->startOfMonth(),
        $monthStartDay->copy()->lastOfMonth())->toArray();
}
```

もっと良い感じにかけると思いますがまぁ1年分できたのでOK