gpt4 book ai didi

PHP错误: Notice: Undefined index: January in

转载 作者:行者123 更新时间:2023-11-29 07:58:48 26 4
gpt4 key购买 nike

我有这个代码来统计本月发布的新闻数量:

$months = array("January","February","March","April",
"May","June","July","August","September","October",
"November","December");
$mews = DB->fetch("SELECT id, YEAR(FROM_UNIXTIME(date)) AS YEAR,
MONTHNAME(FROM_UNIXTIME(date)) AS MONTH,
COUNT(id) AS TOTAL
FROM article
WHERE YEAR(FROM_UNIXTIME(date)) = 2014
GROUP BY YEAR, MONTH ORDER BY YEAR, MONTH");

// Index article counts by month and year for easy lookup
$indexedNewsData = array();
foreach ($mews as $news) {
$indexedNewsData[$news['MONTH']] = $news['TOTAL'];
}
// Then print output
foreach($mews AS $news){

foreach ($months as $month) {
$total = intval($indexedNewsData[$month]);
echo '<li>'.$month.' '.$total.'</li>';
}
}

但是,对于每个月的 0 帖子,我都会看到此错误:

注意:未定义索引:一月......

我该如何解决这个错误?!

演示:http://stackdemo.url.ph/test/

来源:http://stackdemo.url.ph/test/test

最佳答案

您应该正确初始化您的数组。

$indexedNewsData = array();
foreach($months as $month) $indexedNewsData[$month] = 0;

foreach($mews as $news)
...

关于PHP错误: Notice: Undefined index: January in,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24426432/

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