在不知道密钥的情况下在多维数组上执行if语句

时间:2014-01-19 01:29:20

标签: php arrays multidimensional-array

首先让我试着解释一下我要做的事情。

我有一个日历,其中包含存储在数据库中的事件,每个事件都是工作日。我在数据库中保存的重要事项是用户,日期,一年中的一周(1-52)和工作小时数。现在我想通过将第1,2,3,......,51和52周的工作小时数分组来计算薪水。我到目前为止的代码是:

<?php
include ('require.php');
include ('dblink.php');
$time =[];
$results = mysqli_query($con,"SELECT * FROM events WHERE user = '".$_SESSION['s_username']."'") or die(mysqli_error());
if (mysqli_num_rows($results) > 0){
    while($row = mysqli_fetch_assoc($results)){
           if($row['week'] % 2 == 0){
            $week = $row['week'] - 1; // if its an even week then -1 to make it an odd week to add up the hours of the two weeks together.
        }else{
            $week = $row['week'];
        }
        if (isset($time['week']) && $time['week'] == $week){
            $time['hours'] += $row['hours']; //If the previous week was added then add the second week to it.
        }else{
            $year = explode("-", $row['date']);
            $time[] = array('week' => $week, 'hours' => $row['hours'], 'year' => $year[0]);
        }
    }
}
foreach($time as $indTime){
    echo $indTime['hours'].' hours were spent on the '.$indTime['week'].' week of '.$indTime['year'] . '<br />'; 
}
?>

问题出现在第13行,我有一个if语句。我知道这是不正确的,因为我的数组是多维的,并且没有定义$time['week']。但我不知道钥匙,所以我不能这么说,$time[23]['week']

尝试进一步解释我的意思让我展示一下我的输出示例。

4.75 hours were spent on the 41 week of 2013
7.5 hours were spent on the 41 week of 2013
4 hours were spent on the 43 week of 2013
7.5 hours were spent on the 43 week of 2013
7.5 hours were spent on the 43 week of 2013
4.5 hours were spent on the 43 week of 2013
6 hours were spent on the 43 week of 2013
4 hours were spent on the 43 week of 2013
7.5 hours were spent on the 43 week of 2013
7 hours were spent on the 45 week of 2013
8 hours were spent on the 45 week of 2013
8 hours were spent on the 45 week of 2013
4.5 hours were spent on the 45 week of 2013
6 hours were spent on the 45 week of 2013
7.5 hours were spent on the 45 week of 2013
5.5 hours were spent on the 45 week of 2013
8 hours were spent on the 45 week of 2013

由于我的if语句不起作用,它只为数据库中的每一行创建一个新数组。但实际上我想要它做的是将每周的时间相加。因此,所有第41周都将加在一起,例如,小时数将是12.25。谁知道我能做什么?

2 个答案:

答案 0 :(得分:2)

根据您目前所说的话(如果我错了,请纠正我),您需要group来自week,特定users的所有小时?

如果这是正确的,在WEEK字段上添加GROUP BY子句,应该会得到可以给你所需的结果。

我刚写了一个sqlFiddle,所以你可以看到我的意思:http://sqlfiddle.com/#!2/f49a3/2

如果这是期望的最终结果,通过向SQL添加user,您将能够按周分组AND顺序,从而减少数小时的运行。

如果我错了,请告诉我一个解释,我会再试一次:)


编辑:

如果以上是正确的,那么通过更改SQL,您应该能够将代码缩减为类似的内容;

<?php
include ('require.php');
include ('dblink.php');

$sql     = "SELECT `week`, `year` SUM(`hours`) AS `weekly_hours` FROM `events` WHERE `user` = '".$_SESSION['s_username']."' GROUP BY `week` ORDER BY `week` DESC";
$results = mysqli_query($con, $sql) or die(mysqli_error());
if (mysqli_num_rows($results) > 0)
{
    while($row = mysqli_fetch_assoc($results))
    {
        echo number_format($row['weekly_hours'], 2) . ' hours were spent on the ' . $row['week'] . ' week of ' . $row['year'] . '<br />'; 
    }
}
?>

答案 1 :(得分:1)

不使用自动递增的数组键,而是使用周数作为键:

else{
    $year = explode("-", $row['date']);
    $time[$week] = array('week' => $week,'hours' => $row['hours'],'year' => $year[0]);
}

除此之外:写$year = explode("-", $row['date'], 2)[0](PHP&gt; = 5.4)并直接使用$year或者甚至$year = (int)$row['date']可能会更准确,因为无论如何都要假设格式

这使您可以“知道”用于索引多维数组的键,以便合并工时:

if (isset($time[$week])){
    $time[$week]['hours'] += $row['hours'];
}

这将解决PHP中的问题。但是,如果可能的话,最好用SQL表示操作,让数据库进行分组和小时的总和。