Hello,
I want to set the number of months in the following array key fields and have 0 zero value in the value field for the non sales months.
How is this done?
$sql = $db->prepare( " SELECT product, COUNT(*), MONTH(date) FROM table WHERE YEAR(date)='2022' GROUP BY product, MONTH(date) ORDER BY MONTH(date) ASC, COUNT(*) DESC " );
$sql->execute();
$sql->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_COLUMN);
With the above query the following server is giving
Array
(
[product_1] => Array
(
[0] => 17
[1] => 33
[2] => 14
[3] => 36
[4] => 26
[5] => 17
[6] => 18
[7] => 24
[8] => 23
[9] => 51
[10] => 30
[11] => 23
)
[product_2] => Array
(
[0] => 10
[1] => 20
[2] => 11
[3] => 7
[4] => 6
[5] => 9
[6] => 3
[7] => 2
[8] => 10
[9] => 8
[10] => 6
[11] => 8
)
[product_3] => Array
(
[0] => 5
[1] => 4
[2] => 4
[3] => 6
[4] => 1
[5] => 2
[6] => 2
[7] => 6
[8] => 4
[9] => 2
[10] => 2
[11] => 3
)
[product_4] => Array
(
[0] => 2
[1] => 3
[2] => 2
[3] => 3
[4] => 2
[5] => 3
[6] => 1
[7] => 2
[8] => 2
[9] => 4
[10] => 4
[11] => 3
)
[product_5] => Array
(
[0] => 1
[1] => 3
[2] => 3
[3] => 2
[4] => 6
[5] => 4
[6] => 1
[7] => 2
[8] => 1
[9] => 4
[10] => 4
[11] => 3
)
[product_6] => Array
(
[0] => 1
[1] => 3
)
[product_7] => Array
(
[0] => 2
)
)
Sample array
[product_6] => Array
(
[1] => 0
[2] => 1
[3] => 0
[4] => 0
[5] => 0
[6] => 3
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
[12] => 0
)