heidelbeere
heidelbeere

Reputation: 157

PHP Associative Array, same code on same level doesn't work

I have a really strange problem over here. Sorry for the ugly redundant parts, but it is only for prototyping. In both blocks I calculate the seconds of the given worktime. The first block was the first version and worked, but wasn't that robust. So the second block solved the robustness problem. The important part is $hours[$row["gamemaster"]] = $hours[$row["gamemaster"]] + $worktime;

In both blocks the integer $worktime is in seconds. But now the problem is, that in the second block the sum of the worktime doesn't work. It's on the same level, and there are no changes at other parts of the code.

The expected value is the sum of the working hours for each gamemaster. In the first block the printed value is the time as expected and in the second block nothing happens. And my big question mark is, that the calculation of the sum is exactly the same and only the calculation of the worktime before is different. And if I add only normal integers in the second block like $hours[$row["gamemaster"]] = $hours[$row["gamemaster"]] + 1; nothing happens, but for the first block, also something strange like $hours[$row["gamemaster"]] = $hours[$row["gamemaster"]] + "p"; worked.

first block:

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;
  }

output first block for $hours:
[key]:[value]
Felix:8778047400
Marie:2926018800

second block:

  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;
  }

output second block for $hours: Nothing, like nothing is written in the array

Update 1: I reduce the code of the second block to

    $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:

I reduce the code of the second block to

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

There are 8 iterations over the dataset and the output for $worktime afer each iteration is:
1: 7200
2: 5400
3: 5400
4: 7200
5: 7200
6: 7200
7: 5400
8: 5400

But there is still no entry in $hours.

Upvotes: 2

Views: 88

Answers (1)

Gavin
Gavin

Reputation: 6394

The issue is you're overwriting the $hours variable within your sscanf call.

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

Upvotes: 1

Related Questions