简体   繁体   English

如何使用PHP在嵌套的if-else条件下继续进行外部while循环?

[英]How to continue the outer while loop in a nested if-else condition with PHP?

I have a piece of code as following: 我有一段代码如下:

while ( $conver = mysqli_fetch_assoc($converQ)) // for each conversation
{
    $group_mark = array();

    if ($conver['send_from'] == 1)  // send from group
    {  
        if (in_array($conver['sender_id'], $group_mark)) 
        {
            continue;
        }
        else
        {
            array_push($group_mark, $conver['sender_id']);
        }
     ...

In the beginning of the while-loop , I have to check whether the conversation is from group or individual. while-loop的开始,我必须检查对话是来自组还是来自个人。 If it is from group, I should check whether the id has appeared or not. 如果来自组,则应检查id是否已出现。 But now the continue can not stop executing the following code in the while-loop and start the next loop. 但是现在, continue无法停止在while-loop执行以下代码并开始下一个循环。 I think the reason is that its outer function is if-else but not the while-loop directly. 我认为原因是它的外部函数是if-else而不是while-loop Could you please tell me how to edit the code to do what I want? 您能告诉我如何编辑代码以完成我想要的吗? Thank you in advance! 先感谢您!

EDIT: 编辑:

I have tried this: continue 2 but it doesn't work. 我已经尝试过了: continue 2但是不起作用。 Why? 为什么?

Thanks to @ThinkingMedia, I know what's the problem now. 感谢@ThinkingMedia,我知道现在出了什么问题。 I put the $group_mark in the wrong place. 我将$group_mark放在错误的位置。 Sorry. 抱歉。

In these cases which you want to stop check other rows you can develop a function and return as true in the location which you need: 在这些情况下,您想停止检查其他行,则可以开发一个函数并在所需的位置按真返回:

function check()
{
    while ( $conver = mysqli_fetch_assoc($converQ)) // for each conversation
    {
     $group_mark = array();

     if ($conver['send_from'] == 1)  // send from group
     {  
         if (in_array($conver['sender_id'], $group_mark)) 
         {
            return true;
         }
         else
         {
             array_push($group_mark, $conver['sender_id']);
         }
     }
   }
 }

Your code is right. 您的代码是正确的。 But continue does not jump off those if/else statements. 但是continue不会跳过那些if/else语句。

In php docs they say is deprecated: continue {var} 在php docs中,他们说已弃用: continue {var}

5.4.0 Removed the ability to pass in variables (eg, $num = 2; continue $num;) as the numerical argument. 5.4.0删除了将变量(例如$ num = 2;继续$ num;)作为数值参数传递的功能。

You can modify your code as follows: 您可以按以下方式修改代码:

while ( $conver = mysqli_fetch_assoc($converQ)) // for each conversation
{
    $group_mark = array();

    if ($conver['send_from'] == 1)  // send from group
    {  
        if (!in_array($conver['sender_id'], $group_mark)) {
            array_push($group_mark, $conver['sender_id']);
        }
    }
    ...
}

Found some interesting, curious about why your $group_mark = array(); 发现了一些有趣的信息,好奇为什么$ group_mark = array(); is in the while loop. 在while循环中。 It seem like this will get overwrited and will not able to hit the if condition, because of the group_mark always is empty array. 由于group_mark始终为空数组,因此似乎将被覆盖并且无法满足if条件。

Try to move this to before of your while loop. 尝试将其移动到while循环之前。 This should solve and get what you need. 这应该解决并得到您所需要的。

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

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