I have the following format of php array:
$data = array(
array(
'label' => 'Sale',
'color' => '#28d8b2',
'data' =>
array(
array('Jan', 27),
array('Feb', 82),
array('Mar', 56),
array('Apr', 14),
array('May', 28),
array('Jun', 77),
array('Jul', 23),
array('Aug', 49),
array('Sep', 81),
array('Oct', 20),
array('Nov', 23),
array('Dec', 99)
)
)
);
And I want to put my data that query from mysql table to the sub array above as follow:
array(
array('Jan', 27),
array('Feb', 82),
array('Mar', 56),
array('Apr', 14),
array('May', 28),
array('Jun', 77),
array('Jul', 23),
array('Aug', 49),
array('Sep', 81),
array('Oct', 20),
array('Nov', 23),
array('Dec', 99)
)
I tried to put those data that query from mysql database with the following code but still not what I want:
$get_date = dbQuery("SELECT MONTH(STR_TO_DATE(ors.order_date, '%Y-%m-%d')) AS month, SUM(ord.total_price) AS total FROM orders ors INNER JOIN order_detail ord ON (ors.order_id = ord.order_id) WHERE YEAR(STR_TO_DATE(ors.order_date, '%Y-%m-%d')) = '$year' GROUP BY month");
$count = $get_date->rowCount();
$index = 1;
$ar = array();
while($row = $get_date->fetch(PDO::FETCH_ASSOC))
{
$ar[$index] = $row;
$index++;
}
print_r($ar);
So my question is I want to put my data to the existing php array as I mentioned above, even if any month not available in mysql database the script still need to give that month as 0.
e.g if any month not exist in database then put data to the array like this
array(
array('Jan', 27),
array('Feb', 0),
array('Mar', 56),
array('Apr', 14),
array('May', 0),
array('Jun', 77),
array('Jul', 0),
array('Aug', 0),
array('Sep', 81),
array('Oct', 0),
array('Nov', 23),
array('Dec', 0)
)
array_push($data[0]['data'], [$month_name, $month_value]);
Perhabs this will work. Have not tested it.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加