Level 27
@bashy for a collection that has the following data items:
=> Illuminate\Support\Collection {#822
all: [
[
"month" => 4,
"count" => 10,
],
[
"month" => 6,
"count" => 8,
],
[
"month" => 10,
"count" => 11,
],
],
}
You can achieve the same result using (code is copied form the terminal):
$array_months = $collection->pluck('count', 'month')->toArray() + array_fill(1, 12, 0);
=> [
4 => 10,
6 => 8,
10 => 11,
1 => 0,
2 => 0,
3 => 0,
5 => 0,
7 => 0,
8 => 0,
9 => 0,
11 => 0,
12 => 0,
]
>>> ksort($array_months);
=> true
>>> $array_months
=> [
1 => 0,
2 => 0,
3 => 0,
4 => 10,
5 => 0,
6 => 8,
7 => 0,
8 => 0,
9 => 0,
10 => 11,
11 => 0,
12 => 0,
]
>>>
Usman.