我这里有一个非常奇怪的问题。很抱歉这些丑陋的冗余部件,但它只适用于原型设计。在两个块中,我计算给定工作时间的秒数。第一个版本是第一个版本并且工作,但不是那么强大。所以第二个块解决了鲁棒性问题。
重要的是
$hours[$row["gamemaster"]] = $hours[$row["gamemaster"]] + $worktime;
在两个块中,整数$ worktime以秒为单位。但现在问题是,在第二个块中,工作时间的总和不起作用。它处于同一级别,代码的其他部分没有变化。
预期值是每个游戏管理员的工作时间总和。在第一个块中,打印值是预期的时间,在第二个块中没有任何反应。我的大问号是,总和的计算完全相同,只有之前工作时间的计算是不同的。如果我在第二个块中只添加正常整数,如$hours[$row["gamemaster"]] = $hours[$row["gamemaster"]] + 1;
则没有任何反应,但对于第一个块,还有像$hours[$row["gamemaster"]] = $hours[$row["gamemaster"]] + "p";
这样奇怪的东西。
第一块:
if($row["no_game"]==0) {
$worktime = strtotime($row["additional"]);
$worktime = $worktime + 5400;
$hours[$row["gamemaster"]] = $hours[$row["gamemaster"]] + $worktime;
}
else {
$worktime = strtotime($row["additional"]);
$hours[$row["gamemaster"]] = $hours[$row["gamemaster"]] + $worktime;
}
以小时为单位输出第一个块:
[关键]:[价值]
费利克斯:8778047400
玛丽:2926018800
第二块:
if($row["no_game"]==0) {
$str_time = $row["additional"];
$str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);
sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
$time_seconds = $hours * 3600 + $minutes * 60 + $seconds;
$worktime = $time_seconds + 5400;
$hours[$row["gamemaster"]] = $hours[$row["gamemaster"]] + $worktime;
}
else {
$str_time = $row["additional"];
$str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);
sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
$worktime = $hours * 3600 + $minutes * 60 + $seconds;
$hours[$row["gamemaster"]] = $hours[$row["gamemaster"]] + $worktime;
}
输出第二个块$ $: 什么都没有,就像在数组中写的一样
更新1: 我将第二个块的代码减少到
$str_time = $row["additional"];
$str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);
sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
$time_seconds = $hours * 3600 + $minutes * 60 + $seconds;
$worktime = $time_seconds + 5400;Update 1:
我将第二个块的代码减少到
$str_time = $row["additional"];
$str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);
sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
$time_seconds = $hours * 3600 + $minutes * 60 + $seconds;
$worktime = $time_seconds + 5400;
$hours[$row["gamemaster"]] = $hours[$row["gamemaster"]] + $worktime;
数据集上有8次迭代,每次迭代的$ worktime输出为:
1:7200
2:5400
3:5400
4:7200
5:7200
6:7200
7:5400
8:5400
但是在几小时内仍然没有进入。
答案 0 :(得分:1)
问题是您正在覆盖$hours
来电中的sscanf
变量。
$rows = [
['gamemaster' => 1, 'additional' => '00:30:00', 'no_game' => 0],
['gamemaster' => 2, 'additional' => '00:10:30', 'no_game' => 0],
['gamemaster' => 3, 'additional' => '00:20:00', 'no_game' => 0],
['gamemaster' => 1, 'additional' => '00:40:40', 'no_game' => 1],
['gamemaster' => 2, 'additional' => '00:15:00', 'no_game' => 1],
['gamemaster' => 3, 'additional' => '00:00:30', 'no_game' => 1],
['gamemaster' => 1, 'additional' => '00:00:10', 'no_game' => 0],
['gamemaster' => 2, 'additional' => '00:20:30', 'no_game' => 0],
['gamemaster' => 3, 'additional' => '00:00:00', 'no_game' => 0],
['gamemaster' => 1, 'additional' => '00:05:00', 'no_game' => 1],
['gamemaster' => 2, 'additional' => '00:09:40', 'no_game' => 1],
['gamemaster' => 3, 'additional' => '00:18:30', 'no_game' => 1],
['gamemaster' => 1, 'additional' => '00:34:30', 'no_game' => 0],
['gamemaster' => 2, 'additional' => '00:59:59', 'no_game' => 0],
['gamemaster' => 3, 'additional' => '00:40:00', 'no_game' => 0]
];
$result = array();
foreach ($rows as $row) {
$str_time = preg_replace('/^([\d]{1,2})\:([\d]{2})$/', "00:$1:$2", $row['additional']);
sscanf($str_time, '%d:%d:%d', $hours, $minutes, $seconds);
$worktime = $hours * 3600 + $minutes * 60 + $seconds;
if (!$row['no_game']) {
$worktime += 5400;
}
if (!isset($hours[$row['gamemaster']])) {
$result[$row['gamemaster']] = 0;
}
$result[$row['gamemaster']] += $worktime;
}
var_dump($result);