我编写了一个小脚本,它应该创建一个数组,看起来像这样:
array(1 => array( 'month' => 'Jan',
'salary' => '01/31/2013',
'bonus' => '02/15/2013'),
2 => array('month' => '',...));
您得到了基本的思想:主数组中的索引是月份(数字),每个月份包含一个将被动态填充的数组。month
键取决于用户请求的语言,工资和奖金被分配到工资和/或奖金支付的日期。到目前为止,没有什么令人惊讶的。
要获得该数组的基本结构,我认为这将是最简单的:
$this->months = array_fill_keys(range(1,12), array( 'month' => null,
'salary' => null,
'bonus' => null));
然后填充数组,一切都一样顺利,直到我想要将数据写入文件的那一点,我喜欢这样做:
private function writeFile()
{
foreach($this->months as $key => $vals)
{
if ($vals['month'] === null)
{//Only filled from date x to date y, some months can be empty
continue;
}
//this seems to raise notices?
if ($vals['salary'] === null)
{
$vals['salary'] = 'Does not apply';
}
fwrite($this->file, implode(',', $vals).PHP_EOL);
}
fclose($this->file);
return $this;
}
检查salary
是否为空的行会引起注意:"警告:未定义的索引工资"。现在,我不得不把这个添加到代码中:
if (!array_key_exists('salary', $vals) || $vals['salary'] === null)
{
if (!array_key_exists('bonus', $vals) || $vals['bonus'] === null)
{
break;
}
$vals['salary'] = 'Does not apply';
}
得到我需要的结果。我在谷歌上搜索了一下,偶然发现了这个bug报告,上次修改是在4年前(2009-05-08),但状态仍然设置为"无反馈"。
其他人遇到过类似的故障/错误吗?还是我遗漏了什么?我怎样才能避免这个问题,而不需要太多的if
和函数调用,而不需要改变我的设置(E_STRICT | E_ALL
,应该是)。
顺便说一句:我在Slackware 14上运行PHP 5.4.7。对于这个小应用程序,我使用了2个Symfony组件(ClassLoader和Console),但由于这是一个与Symfony无关的对象的一部分,除了通过UniversalClassLoader
加载外,我认为这无关紧要。
因为这个bug据说是PDO
相关的:是的,我正在使用PDO,但在另一个类中。
我不确定,但尝试使用
$this->months = array_fill(1,12, array( 'month' => null,
'salary' => null,
'bonus' => null));
在几个var_dump
之后,我发现了原因:数组键是range(1,12)
,以确定我正在处理的是哪个月。为此,我以以下方式使用了DateTime
对象:
$date->modify('+ '.$diff.' days');
$key = $date->format('m');
问题是format
调用返回一个字符串。目的是列出工资和奖金的发放时间。奖金必须在每个15号支付,如果15号是周六或周日,则在下周三支付。工资将在每月的最后一天或最后一个星期五发放。
换句话说,奖金支付日期是这样分配的:
$key = $date->format('m');
$this->months[$key]['month'] = $date->format('M');
if ($date->format('d') == 15)
{
//find find week-day (15th or following Wednesday)
$this->months[--$key]['bonus'] = $date->format('m/d/Y');
$key++;
//set date to end of month
}
//check week-day, and modify date if required
$this->months[$key]['salary'] = $date->format('m/d/Y');
因为$this->months
数组的键是数字,但$key
使用的格式是一个2位数的字符串,前导零,我遇到了问题。
每个月的15号,$key
的值被强制转换为整数(自减/自增运算符),但是使用字符串赋值月份。
我在原来的问题中提供的信息是不充分的,很抱歉,我刚刚熬夜了。最后的修复非常简单:
$key = (int) $date->format('m');//cast
我真诚地感谢所有的回复,以及每个为SO社区做出贡献的人。我本来会删掉这个问题的,但如果没有人反对,我想我可能会把它留作我愚蠢的证明。