PHP loop through array, sort by week and count the weekly working hours
Im looping through my childpages and I get the table but now I need to sort and sum the working hours for each week.
This is my code so far:
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$dow = date('w', strtotime($date));
$week = (int)$week;
?>
<tr>
<td><b>(<?php echo $week;?>) <?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }?>
The output looks like this:
As I explained I'm trying to sum the hours for each week.
I have tried with if and while statements but can't get it to work.
Any suggestions?
Thank you!
EDIT:
I get the weeks separated:
$last_date =null;
$weeklyhours = 0;
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$week = (int)$week;
?>
<?php
if ( $last_date != $week ) {
$weeklyhours = $h->worktime + $weeklyhours;?>
<tr style="background: #f8f8f8;">
<td>Vecka: <?= $week;?></td>
<td></td>
<td></td>
<td><?= $weeklyhours;?></td>
<td></td>
</tr>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
else if ( $last_date == $week ){
$weeklyhours = $h->worktime + $weeklyhours;
?>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
The problem now is that I can't get the summary of the hours per each week.
Any pointers?
Thanks!
EDIT:
$weeklyhours = 0;
// NEW LINE HERE - Create an array to use.
$hoursPerWeek = ;
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$week = (int)$week;
// NEW LINE HERE - Add to array.
$hoursPerWeek[$week] += $h->worktime;
?>
<?php
if ( $last_date != $week ) {?>
<tr style="background: #f8f8f8;">
<td>Vecka: <?= $week;?></td>
<td></td>
<td></td>
<td><?= $hoursPerWeek[$week];?></td>
<td></td>
</tr>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
else if ( $last_date == $week ){?>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
//echo $h->workdate."-".$weeklyhours;
$last_date = $week;
?>
<?php }?>
php foreach processwire
add a comment |
Im looping through my childpages and I get the table but now I need to sort and sum the working hours for each week.
This is my code so far:
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$dow = date('w', strtotime($date));
$week = (int)$week;
?>
<tr>
<td><b>(<?php echo $week;?>) <?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }?>
The output looks like this:
As I explained I'm trying to sum the hours for each week.
I have tried with if and while statements but can't get it to work.
Any suggestions?
Thank you!
EDIT:
I get the weeks separated:
$last_date =null;
$weeklyhours = 0;
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$week = (int)$week;
?>
<?php
if ( $last_date != $week ) {
$weeklyhours = $h->worktime + $weeklyhours;?>
<tr style="background: #f8f8f8;">
<td>Vecka: <?= $week;?></td>
<td></td>
<td></td>
<td><?= $weeklyhours;?></td>
<td></td>
</tr>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
else if ( $last_date == $week ){
$weeklyhours = $h->worktime + $weeklyhours;
?>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
The problem now is that I can't get the summary of the hours per each week.
Any pointers?
Thanks!
EDIT:
$weeklyhours = 0;
// NEW LINE HERE - Create an array to use.
$hoursPerWeek = ;
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$week = (int)$week;
// NEW LINE HERE - Add to array.
$hoursPerWeek[$week] += $h->worktime;
?>
<?php
if ( $last_date != $week ) {?>
<tr style="background: #f8f8f8;">
<td>Vecka: <?= $week;?></td>
<td></td>
<td></td>
<td><?= $hoursPerWeek[$week];?></td>
<td></td>
</tr>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
else if ( $last_date == $week ){?>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
//echo $h->workdate."-".$weeklyhours;
$last_date = $week;
?>
<?php }?>
php foreach processwire
add a comment |
Im looping through my childpages and I get the table but now I need to sort and sum the working hours for each week.
This is my code so far:
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$dow = date('w', strtotime($date));
$week = (int)$week;
?>
<tr>
<td><b>(<?php echo $week;?>) <?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }?>
The output looks like this:
As I explained I'm trying to sum the hours for each week.
I have tried with if and while statements but can't get it to work.
Any suggestions?
Thank you!
EDIT:
I get the weeks separated:
$last_date =null;
$weeklyhours = 0;
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$week = (int)$week;
?>
<?php
if ( $last_date != $week ) {
$weeklyhours = $h->worktime + $weeklyhours;?>
<tr style="background: #f8f8f8;">
<td>Vecka: <?= $week;?></td>
<td></td>
<td></td>
<td><?= $weeklyhours;?></td>
<td></td>
</tr>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
else if ( $last_date == $week ){
$weeklyhours = $h->worktime + $weeklyhours;
?>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
The problem now is that I can't get the summary of the hours per each week.
Any pointers?
Thanks!
EDIT:
$weeklyhours = 0;
// NEW LINE HERE - Create an array to use.
$hoursPerWeek = ;
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$week = (int)$week;
// NEW LINE HERE - Add to array.
$hoursPerWeek[$week] += $h->worktime;
?>
<?php
if ( $last_date != $week ) {?>
<tr style="background: #f8f8f8;">
<td>Vecka: <?= $week;?></td>
<td></td>
<td></td>
<td><?= $hoursPerWeek[$week];?></td>
<td></td>
</tr>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
else if ( $last_date == $week ){?>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
//echo $h->workdate."-".$weeklyhours;
$last_date = $week;
?>
<?php }?>
php foreach processwire
Im looping through my childpages and I get the table but now I need to sort and sum the working hours for each week.
This is my code so far:
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$dow = date('w', strtotime($date));
$week = (int)$week;
?>
<tr>
<td><b>(<?php echo $week;?>) <?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }?>
The output looks like this:
As I explained I'm trying to sum the hours for each week.
I have tried with if and while statements but can't get it to work.
Any suggestions?
Thank you!
EDIT:
I get the weeks separated:
$last_date =null;
$weeklyhours = 0;
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$week = (int)$week;
?>
<?php
if ( $last_date != $week ) {
$weeklyhours = $h->worktime + $weeklyhours;?>
<tr style="background: #f8f8f8;">
<td>Vecka: <?= $week;?></td>
<td></td>
<td></td>
<td><?= $weeklyhours;?></td>
<td></td>
</tr>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
else if ( $last_date == $week ){
$weeklyhours = $h->worktime + $weeklyhours;
?>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
The problem now is that I can't get the summary of the hours per each week.
Any pointers?
Thanks!
EDIT:
$weeklyhours = 0;
// NEW LINE HERE - Create an array to use.
$hoursPerWeek = ;
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$week = (int)$week;
// NEW LINE HERE - Add to array.
$hoursPerWeek[$week] += $h->worktime;
?>
<?php
if ( $last_date != $week ) {?>
<tr style="background: #f8f8f8;">
<td>Vecka: <?= $week;?></td>
<td></td>
<td></td>
<td><?= $hoursPerWeek[$week];?></td>
<td></td>
</tr>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
else if ( $last_date == $week ){?>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }
//echo $h->workdate."-".$weeklyhours;
$last_date = $week;
?>
<?php }?>
php foreach processwire
php foreach processwire
edited 2 days ago
asked 2 days ago
Simon Gottberg
55
55
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
A simple and easy way to "sum" the hours per week is by having an array that has the week as the key, and the value as the sum of hours.
In your example i would in your 'foreach' do following:
<?php
// NEW LINE HERE - Create an array to use.
$hoursPerWeek = ;
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$dow = date('w', strtotime($date));
$week = (int)$week;
// NEW LINE HERE - Add to array.
$hoursPerWeek[$week] += $h->worktime;
?>
<tr>
<td><b>(<?php echo $week;?>) <?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }?>
So what I'm doing here is to store the hours in an array, on each key which is your week.
To avoid some PHP warnings, you might wanne make sure key on the array is created before you add value to it..
Hope it makes sense.
EDIT: To update your code, to fix your problem, and also to clean your code abit, I think this would do it for you.
<?php
// Our data source.
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
// We need to loop through the array first to use the summed up value later.
$hoursPerWeek = ;
foreach ($hours as $h) {
$hoursPerWeek[$week] += $h->worktime;
}
$last_date = 0;
$weeklyhours = 0;
// Loop hours again, now make the output.
foreach ($hours as $h) {
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$week = (int)$week;
// Previous, was the last of that week.
if ($last_date != $week) {
?>
<tr style="background: #f8f8f8;">
<td>Vecka: <?= $week;?></td>
<td></td>
<td></td>
<td><?=$hoursPerWeek[$week];?></td>
<td></td>
</tr>
<?php
}
$last_date = $week;
// Create a output for our current hourlog.
?>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php
}
?>
Thank you @Aprilsnar! I get the idea, but I have a problem when I get the same weeks multiple times in the foreach loop. Eg. I get 49,49, 50,50, 51,51,51 etc. I tried to use the array_unique with implode but can't get the weeks only once. $weeks = implode(' ',array_unique(explode(' ', $week)));
– Simon Gottberg
2 days ago
If I'm understanding correctly, you want to sum the hours together on the week? The line $hoursPerWeek[$week] += $h->worktime; would sum the together because of the += but I might be misunderstanding your question here..
– Aprilsnar
2 days ago
Yes, but I I echo $hoursPerWeek[$week]; I get the following: 2 5 24 26 38 61 73 2 14 26. Week52 there are 2 worktimes 2, 3 = 5, then week 51 5 worktimes, 24, 2, 12, 23, 12 = 73, week 49 3 worktimes, 2, 12, 12 = 26. So the problems is that it kind sums the hours but it doesn't sum the hours correctly.
– Simon Gottberg
2 days ago
Are you able to post the output of following: var_dump($hoursPerWeek);
– Aprilsnar
2 days ago
Yes, this is my result: array(1) { [52]=> float(2) } array(1) { [52]=> float(5) } array(2) { [52]=> float(5) [51]=> float(24) } array(2) { [52]=> float(5) [51]=> float(26) } array(2) { [52]=> float(5) [51]=> float(38) } array(2) { [52]=> float(5) [51]=> float(61) } array(2) { [52]=> float(5) [51]=> float(73) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(2) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(14) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(26) }
– Simon Gottberg
2 days ago
|
show 5 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945017%2fphp-loop-through-array-sort-by-week-and-count-the-weekly-working-hours%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A simple and easy way to "sum" the hours per week is by having an array that has the week as the key, and the value as the sum of hours.
In your example i would in your 'foreach' do following:
<?php
// NEW LINE HERE - Create an array to use.
$hoursPerWeek = ;
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$dow = date('w', strtotime($date));
$week = (int)$week;
// NEW LINE HERE - Add to array.
$hoursPerWeek[$week] += $h->worktime;
?>
<tr>
<td><b>(<?php echo $week;?>) <?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }?>
So what I'm doing here is to store the hours in an array, on each key which is your week.
To avoid some PHP warnings, you might wanne make sure key on the array is created before you add value to it..
Hope it makes sense.
EDIT: To update your code, to fix your problem, and also to clean your code abit, I think this would do it for you.
<?php
// Our data source.
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
// We need to loop through the array first to use the summed up value later.
$hoursPerWeek = ;
foreach ($hours as $h) {
$hoursPerWeek[$week] += $h->worktime;
}
$last_date = 0;
$weeklyhours = 0;
// Loop hours again, now make the output.
foreach ($hours as $h) {
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$week = (int)$week;
// Previous, was the last of that week.
if ($last_date != $week) {
?>
<tr style="background: #f8f8f8;">
<td>Vecka: <?= $week;?></td>
<td></td>
<td></td>
<td><?=$hoursPerWeek[$week];?></td>
<td></td>
</tr>
<?php
}
$last_date = $week;
// Create a output for our current hourlog.
?>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php
}
?>
Thank you @Aprilsnar! I get the idea, but I have a problem when I get the same weeks multiple times in the foreach loop. Eg. I get 49,49, 50,50, 51,51,51 etc. I tried to use the array_unique with implode but can't get the weeks only once. $weeks = implode(' ',array_unique(explode(' ', $week)));
– Simon Gottberg
2 days ago
If I'm understanding correctly, you want to sum the hours together on the week? The line $hoursPerWeek[$week] += $h->worktime; would sum the together because of the += but I might be misunderstanding your question here..
– Aprilsnar
2 days ago
Yes, but I I echo $hoursPerWeek[$week]; I get the following: 2 5 24 26 38 61 73 2 14 26. Week52 there are 2 worktimes 2, 3 = 5, then week 51 5 worktimes, 24, 2, 12, 23, 12 = 73, week 49 3 worktimes, 2, 12, 12 = 26. So the problems is that it kind sums the hours but it doesn't sum the hours correctly.
– Simon Gottberg
2 days ago
Are you able to post the output of following: var_dump($hoursPerWeek);
– Aprilsnar
2 days ago
Yes, this is my result: array(1) { [52]=> float(2) } array(1) { [52]=> float(5) } array(2) { [52]=> float(5) [51]=> float(24) } array(2) { [52]=> float(5) [51]=> float(26) } array(2) { [52]=> float(5) [51]=> float(38) } array(2) { [52]=> float(5) [51]=> float(61) } array(2) { [52]=> float(5) [51]=> float(73) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(2) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(14) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(26) }
– Simon Gottberg
2 days ago
|
show 5 more comments
A simple and easy way to "sum" the hours per week is by having an array that has the week as the key, and the value as the sum of hours.
In your example i would in your 'foreach' do following:
<?php
// NEW LINE HERE - Create an array to use.
$hoursPerWeek = ;
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$dow = date('w', strtotime($date));
$week = (int)$week;
// NEW LINE HERE - Add to array.
$hoursPerWeek[$week] += $h->worktime;
?>
<tr>
<td><b>(<?php echo $week;?>) <?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }?>
So what I'm doing here is to store the hours in an array, on each key which is your week.
To avoid some PHP warnings, you might wanne make sure key on the array is created before you add value to it..
Hope it makes sense.
EDIT: To update your code, to fix your problem, and also to clean your code abit, I think this would do it for you.
<?php
// Our data source.
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
// We need to loop through the array first to use the summed up value later.
$hoursPerWeek = ;
foreach ($hours as $h) {
$hoursPerWeek[$week] += $h->worktime;
}
$last_date = 0;
$weeklyhours = 0;
// Loop hours again, now make the output.
foreach ($hours as $h) {
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$week = (int)$week;
// Previous, was the last of that week.
if ($last_date != $week) {
?>
<tr style="background: #f8f8f8;">
<td>Vecka: <?= $week;?></td>
<td></td>
<td></td>
<td><?=$hoursPerWeek[$week];?></td>
<td></td>
</tr>
<?php
}
$last_date = $week;
// Create a output for our current hourlog.
?>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php
}
?>
Thank you @Aprilsnar! I get the idea, but I have a problem when I get the same weeks multiple times in the foreach loop. Eg. I get 49,49, 50,50, 51,51,51 etc. I tried to use the array_unique with implode but can't get the weeks only once. $weeks = implode(' ',array_unique(explode(' ', $week)));
– Simon Gottberg
2 days ago
If I'm understanding correctly, you want to sum the hours together on the week? The line $hoursPerWeek[$week] += $h->worktime; would sum the together because of the += but I might be misunderstanding your question here..
– Aprilsnar
2 days ago
Yes, but I I echo $hoursPerWeek[$week]; I get the following: 2 5 24 26 38 61 73 2 14 26. Week52 there are 2 worktimes 2, 3 = 5, then week 51 5 worktimes, 24, 2, 12, 23, 12 = 73, week 49 3 worktimes, 2, 12, 12 = 26. So the problems is that it kind sums the hours but it doesn't sum the hours correctly.
– Simon Gottberg
2 days ago
Are you able to post the output of following: var_dump($hoursPerWeek);
– Aprilsnar
2 days ago
Yes, this is my result: array(1) { [52]=> float(2) } array(1) { [52]=> float(5) } array(2) { [52]=> float(5) [51]=> float(24) } array(2) { [52]=> float(5) [51]=> float(26) } array(2) { [52]=> float(5) [51]=> float(38) } array(2) { [52]=> float(5) [51]=> float(61) } array(2) { [52]=> float(5) [51]=> float(73) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(2) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(14) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(26) }
– Simon Gottberg
2 days ago
|
show 5 more comments
A simple and easy way to "sum" the hours per week is by having an array that has the week as the key, and the value as the sum of hours.
In your example i would in your 'foreach' do following:
<?php
// NEW LINE HERE - Create an array to use.
$hoursPerWeek = ;
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$dow = date('w', strtotime($date));
$week = (int)$week;
// NEW LINE HERE - Add to array.
$hoursPerWeek[$week] += $h->worktime;
?>
<tr>
<td><b>(<?php echo $week;?>) <?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }?>
So what I'm doing here is to store the hours in an array, on each key which is your week.
To avoid some PHP warnings, you might wanne make sure key on the array is created before you add value to it..
Hope it makes sense.
EDIT: To update your code, to fix your problem, and also to clean your code abit, I think this would do it for you.
<?php
// Our data source.
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
// We need to loop through the array first to use the summed up value later.
$hoursPerWeek = ;
foreach ($hours as $h) {
$hoursPerWeek[$week] += $h->worktime;
}
$last_date = 0;
$weeklyhours = 0;
// Loop hours again, now make the output.
foreach ($hours as $h) {
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$week = (int)$week;
// Previous, was the last of that week.
if ($last_date != $week) {
?>
<tr style="background: #f8f8f8;">
<td>Vecka: <?= $week;?></td>
<td></td>
<td></td>
<td><?=$hoursPerWeek[$week];?></td>
<td></td>
</tr>
<?php
}
$last_date = $week;
// Create a output for our current hourlog.
?>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php
}
?>
A simple and easy way to "sum" the hours per week is by having an array that has the week as the key, and the value as the sum of hours.
In your example i would in your 'foreach' do following:
<?php
// NEW LINE HERE - Create an array to use.
$hoursPerWeek = ;
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
foreach ($hours as $h){?>
<?php
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$dow = date('w', strtotime($date));
$week = (int)$week;
// NEW LINE HERE - Add to array.
$hoursPerWeek[$week] += $h->worktime;
?>
<tr>
<td><b>(<?php echo $week;?>) <?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php }?>
So what I'm doing here is to store the hours in an array, on each key which is your week.
To avoid some PHP warnings, you might wanne make sure key on the array is created before you add value to it..
Hope it makes sense.
EDIT: To update your code, to fix your problem, and also to clean your code abit, I think this would do it for you.
<?php
// Our data source.
$hours = $pages->find('template=arbetstimmar, sort='.$sort.','.$find.', userid='.$uid.',workdate>='.$startd.',workdate<='.$stopd);
// We need to loop through the array first to use the summed up value later.
$hoursPerWeek = ;
foreach ($hours as $h) {
$hoursPerWeek[$week] += $h->worktime;
}
$last_date = 0;
$weeklyhours = 0;
// Loop hours again, now make the output.
foreach ($hours as $h) {
$th = $h->worktime + $th; //totalhours
$tp = $h->worktime * $h->priceperhour;
$tc = $tp + $tc;
$date = date("Y-m-d", strtotime($h->workdate));
$week = date('W', strtotime($date));
$week = (int)$week;
// Previous, was the last of that week.
if ($last_date != $week) {
?>
<tr style="background: #f8f8f8;">
<td>Vecka: <?= $week;?></td>
<td></td>
<td></td>
<td><?=$hoursPerWeek[$week];?></td>
<td></td>
</tr>
<?php
}
$last_date = $week;
// Create a output for our current hourlog.
?>
<tr>
<td><b><?= $h->workdate;?></b></td>
<td><a href="<?= $h->parent->url;?>#<?=$h->id;?>"><?php echo $h->parent->parent->parent->title. " > ";echo $h->parent->parent->title. " > "; echo $h->parent->title; ?></a></td>
<td><?= $h->workexplanation;?></td>
<td><?= number_format($h->worktime, 2, '.', '')?>h</td>
<td><?= number_format($h->priceperhour, 2, '.', '')?>€</td>
</tr>
<?php
}
?>
edited yesterday
answered 2 days ago
Aprilsnar
321212
321212
Thank you @Aprilsnar! I get the idea, but I have a problem when I get the same weeks multiple times in the foreach loop. Eg. I get 49,49, 50,50, 51,51,51 etc. I tried to use the array_unique with implode but can't get the weeks only once. $weeks = implode(' ',array_unique(explode(' ', $week)));
– Simon Gottberg
2 days ago
If I'm understanding correctly, you want to sum the hours together on the week? The line $hoursPerWeek[$week] += $h->worktime; would sum the together because of the += but I might be misunderstanding your question here..
– Aprilsnar
2 days ago
Yes, but I I echo $hoursPerWeek[$week]; I get the following: 2 5 24 26 38 61 73 2 14 26. Week52 there are 2 worktimes 2, 3 = 5, then week 51 5 worktimes, 24, 2, 12, 23, 12 = 73, week 49 3 worktimes, 2, 12, 12 = 26. So the problems is that it kind sums the hours but it doesn't sum the hours correctly.
– Simon Gottberg
2 days ago
Are you able to post the output of following: var_dump($hoursPerWeek);
– Aprilsnar
2 days ago
Yes, this is my result: array(1) { [52]=> float(2) } array(1) { [52]=> float(5) } array(2) { [52]=> float(5) [51]=> float(24) } array(2) { [52]=> float(5) [51]=> float(26) } array(2) { [52]=> float(5) [51]=> float(38) } array(2) { [52]=> float(5) [51]=> float(61) } array(2) { [52]=> float(5) [51]=> float(73) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(2) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(14) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(26) }
– Simon Gottberg
2 days ago
|
show 5 more comments
Thank you @Aprilsnar! I get the idea, but I have a problem when I get the same weeks multiple times in the foreach loop. Eg. I get 49,49, 50,50, 51,51,51 etc. I tried to use the array_unique with implode but can't get the weeks only once. $weeks = implode(' ',array_unique(explode(' ', $week)));
– Simon Gottberg
2 days ago
If I'm understanding correctly, you want to sum the hours together on the week? The line $hoursPerWeek[$week] += $h->worktime; would sum the together because of the += but I might be misunderstanding your question here..
– Aprilsnar
2 days ago
Yes, but I I echo $hoursPerWeek[$week]; I get the following: 2 5 24 26 38 61 73 2 14 26. Week52 there are 2 worktimes 2, 3 = 5, then week 51 5 worktimes, 24, 2, 12, 23, 12 = 73, week 49 3 worktimes, 2, 12, 12 = 26. So the problems is that it kind sums the hours but it doesn't sum the hours correctly.
– Simon Gottberg
2 days ago
Are you able to post the output of following: var_dump($hoursPerWeek);
– Aprilsnar
2 days ago
Yes, this is my result: array(1) { [52]=> float(2) } array(1) { [52]=> float(5) } array(2) { [52]=> float(5) [51]=> float(24) } array(2) { [52]=> float(5) [51]=> float(26) } array(2) { [52]=> float(5) [51]=> float(38) } array(2) { [52]=> float(5) [51]=> float(61) } array(2) { [52]=> float(5) [51]=> float(73) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(2) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(14) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(26) }
– Simon Gottberg
2 days ago
Thank you @Aprilsnar! I get the idea, but I have a problem when I get the same weeks multiple times in the foreach loop. Eg. I get 49,49, 50,50, 51,51,51 etc. I tried to use the array_unique with implode but can't get the weeks only once. $weeks = implode(' ',array_unique(explode(' ', $week)));
– Simon Gottberg
2 days ago
Thank you @Aprilsnar! I get the idea, but I have a problem when I get the same weeks multiple times in the foreach loop. Eg. I get 49,49, 50,50, 51,51,51 etc. I tried to use the array_unique with implode but can't get the weeks only once. $weeks = implode(' ',array_unique(explode(' ', $week)));
– Simon Gottberg
2 days ago
If I'm understanding correctly, you want to sum the hours together on the week? The line $hoursPerWeek[$week] += $h->worktime; would sum the together because of the += but I might be misunderstanding your question here..
– Aprilsnar
2 days ago
If I'm understanding correctly, you want to sum the hours together on the week? The line $hoursPerWeek[$week] += $h->worktime; would sum the together because of the += but I might be misunderstanding your question here..
– Aprilsnar
2 days ago
Yes, but I I echo $hoursPerWeek[$week]; I get the following: 2 5 24 26 38 61 73 2 14 26. Week52 there are 2 worktimes 2, 3 = 5, then week 51 5 worktimes, 24, 2, 12, 23, 12 = 73, week 49 3 worktimes, 2, 12, 12 = 26. So the problems is that it kind sums the hours but it doesn't sum the hours correctly.
– Simon Gottberg
2 days ago
Yes, but I I echo $hoursPerWeek[$week]; I get the following: 2 5 24 26 38 61 73 2 14 26. Week52 there are 2 worktimes 2, 3 = 5, then week 51 5 worktimes, 24, 2, 12, 23, 12 = 73, week 49 3 worktimes, 2, 12, 12 = 26. So the problems is that it kind sums the hours but it doesn't sum the hours correctly.
– Simon Gottberg
2 days ago
Are you able to post the output of following: var_dump($hoursPerWeek);
– Aprilsnar
2 days ago
Are you able to post the output of following: var_dump($hoursPerWeek);
– Aprilsnar
2 days ago
Yes, this is my result: array(1) { [52]=> float(2) } array(1) { [52]=> float(5) } array(2) { [52]=> float(5) [51]=> float(24) } array(2) { [52]=> float(5) [51]=> float(26) } array(2) { [52]=> float(5) [51]=> float(38) } array(2) { [52]=> float(5) [51]=> float(61) } array(2) { [52]=> float(5) [51]=> float(73) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(2) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(14) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(26) }
– Simon Gottberg
2 days ago
Yes, this is my result: array(1) { [52]=> float(2) } array(1) { [52]=> float(5) } array(2) { [52]=> float(5) [51]=> float(24) } array(2) { [52]=> float(5) [51]=> float(26) } array(2) { [52]=> float(5) [51]=> float(38) } array(2) { [52]=> float(5) [51]=> float(61) } array(2) { [52]=> float(5) [51]=> float(73) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(2) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(14) } array(3) { [52]=> float(5) [51]=> float(73) [49]=> float(26) }
– Simon Gottberg
2 days ago
|
show 5 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945017%2fphp-loop-through-array-sort-by-week-and-count-the-weekly-working-hours%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown