简体   繁体   English

循环内对象上的php比较运算符

[英]php comparison operators on objects within loops

I have two arrays in php that contain custom class objects. 我在php中有两个包含自定义类对象的数组。 One I iterate through in a while loop, using $h, and the other I access through another variable, $i, inside the while loop for comparison. 我使用$ h在while循环中循环访问,另一个我通过while循环内的另一个变量$ i访问以进行比较。 I only increment $i within the parent loop if a certain condition is met, and there may be multiple copies of the same object within the array, which I need to skip over. 如果满足特定条件,我只会在父循环中递增$ i,并且数组中可能存在同一对象的多个副本,我需要跳过这些副本。

My code looks as follows: 我的代码如下所示:

$h = 0;
$i = 0
$j = 10; // Number determined elsewhere but statically entered for example purposes

while ($h < $j)
{
    if ($audits[$i]->id == $auditIDs[$h])
    {
        // Do Stuff...

        do
        {
            $i++;
        } while ($audits[$i]->id == $audits[$i+1]->id);
    }
    else
    {
        // Do stuff...
    }
    $h++;
}

If I replace the == comparison operator with === it still fails. 如果将==比较运算符替换为===,它仍然会失败。 Replace the do-while loop with a simple IF statement works, however. 但是,用简单的IF语句替换do-while循环即可。

if ($audits[$i]->id == $audits[$i+1]->id)
{
    $i++;
}

But that code does not allow for the possibility of more than one duplicate in the array, which may or may not occur, and I have to increment $i manually before calling the if statement (it must be incremented at least once if the previous if statement evaluates to true). 但是该代码不允许在数组中出现多个重复项,这种重复可能发生也可能不会发生,而且我必须在调用if语句之前手动递增$ i(如果先前的if必须至少递增一次)语句评估为true)。

Can anyone shed some light into the situation? 任何人都可以对情况有所了解吗? I appreciate the help! 感谢您的帮助!

class auditItem
{
    public $login;
    public $points;
    public $date;
    public $onTime;
    public $name;
    public $id;
    public $msg;

    function auditItem($login, $points, $date, $name, $id, $msg)
    {
        $this->login = $login;
        $this->points = $points;
        $this->date = $date;
        $this->name = $name;
        $this->id = $id;
        $this->msg = $msg;
    }
}

In PHP, the do-while loop has special behavior. 在PHP中,do-while循环具有特殊的行为。 It differs from a regular while loop in the fact that the first iteration is guaranteed to always run. 它与常规的while循环的不同之处在于,可以确保第一次迭代始终运行。 In your code, $i has already been incremented by 1 before the comparison check, whereas, in the if statement, it has not. 在您的代码中,$ i在比较检查之前已经增加了1,而在if语句中则没有。

That's the difference in the two blocks of code I noticed. 那就是我注意到的两个代码块之间的差异。 You haven't given enough information about the $audits array to provide a solid solution. 您没有提供有关$ audits数组的足够信息来提供可靠的解决方案。 Perhaps the the second index is out of bounds? 也许第二个索引超出范围?

Why not use a while loop instead of do-while? 为什么不使用while循环而不是do-while? In that case the condition will be first evaluated at $i=0 and incremented afterwards. 在这种情况下,条件将首先在$ i = 0时求值,然后递增。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM