PHP循环数组,按周排序,统计每周工作时间

PHP loop through array, sort by week and count the weekly working hours

我循环浏览我的子页面并得到 table 但现在我需要对每周的工作时间进行排序和求和。

到目前为止,这是我的代码:

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

正如我所解释的,我正在尝试汇总每周的小时数。

我尝试过使用 if 和 while 语句,但无法正常工作。

有什么建议吗?

谢谢!

编辑:

我把周分开了:

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

现在的问题是我无法获取每周的小时数摘要。

有什么指点吗?

谢谢!

编辑:

    $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 }?>

一个简单易行的 "sum" 每周小时数的方法是使用一个数组,该数组以周作为键,值作为小时数的总和。

在你的例子中,我会在你的 'foreach' 中执行以下操作:

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

所以我在这里做的是将时间存储在一个数组中,每个键是你的一周。

为了避免某些 PHP 警告,您可能需要确保在向数组添加值之前创建数组上的键..

希望它有意义。

编辑:要更新您的代码、解决您的问题以及稍微清理您的代码,我认为这会为您完成。

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