如果数组中出现空值,则将零添加到数组 [英] Adding zero to an array if empty values occurs in array
本文介绍了如果数组中出现空值,则将零添加到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是在按日期为每个产品组选择总和(金额)之后得到的数组。
当日期没有对应的金额时显示为空。我需要将其显示为零。
This is the array which I am getting after selecting sum(amounts) for each products group by date. when the dates did not having correspondent amount it is displaying as empty.I need to display it as zero.
Array
(
[2019-05-16] => Array
(
[0] => 499
)
[2019-05-17] => Array
(
[0] => 1998
)
[2019-05-18] => Array
(
[0] => 195
)
[2019-05-19] => Array
(
[0] => 1194
[1] => 999
)
)
但是输出应该是这样,
有没有办法将空值设为0
Is there any way to make empty values as 0
Array
(
[2019-05-16] => Array
(
[0] => 499
[1] => 0
)
[2019-05-17] => Array
(
[0] => 1998
[1] => 0
)
[2019-05-18] => Array
(
[0] => 195
[1] => 0
)
[2019-05-19] => Array
(
[0] => 1194
[1] => 999
)
)
这是脚本
foreach ($productid as $itemid) {
$query1 = "select date, SUM(amount) as amount from app_product_details where date between '2019-05-14 00:00:00' and '2019-05-20 23:59:59' and appid = $itemid group by date";
$getquery1 = mysqli_query($conn,$query1);
while($getcount1 = mysqli_fetch_assoc($getquery1))
{
$grset1[] = $getcount1;
}
}
foreach($grset1 as $r){
$dates1[$r['date']][] = $r['amount'];
}
请帮助我获得输出。
推荐答案
您可以更改查询,以在没有指定产品的情况下返回0值,而不是尝试破解数组当天卖出。这应该起作用:
Rather than trying to hack your array, you can change your query so that it returns a 0 value when there were none of a given product sold on that day. This should work:
$query1 = "SELECT date, SUM(CASE WHEN appid = $itemid THEN amount ELSE 0 END) AS amount
FROM app_product_details
WHERE date BETWEEN '2019-05-14 00:00:00' AND '2019-05-20 23:59:59'
GROUP BY date";
这篇关于如果数组中出现空值,则将零添加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文