gpt4 book ai didi

php - 如果数组中出现空值,则向数组添加零

转载 作者:行者123 更新时间:2023-11-29 09:43:01 25 4
gpt4 key购买 nike

这是我按日期为每个产品组选择总和(金额)后得到的数组。当日期没有对应的金额时,它显示为空。我需要将其显示为零。

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


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 值,而不是尝试破解您的数组。这应该有效:

$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";

关于php - 如果数组中出现空值,则向数组添加零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56223598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com