【问题标题】:PHP For Loop Does Not Seem To Be WorkingPHP For 循环似乎不工作
【发布时间】:2018-05-29 22:25:41
【问题描述】:

我正在制作一个控制面板来使用 Telegram Bot API 和 PHP 管理我的 Telegram Bot。

基本上,我想在一个小聊天框中显示单个用户的每条消息。

因为可能有更多用户向 Bot 发送了消息,所以我必须检查发件人的 user_id 是否没有重复并再次重复,然后为新发件人创建一个新的聊天框。

为了做到这一点,我获取了result 中的数组数量并执行了以下操作:

PRIVATE CODE

正如你在代码开头看到的那样,我创建了变量 store_id 来保存第一个 sender_id,如果这个 sender_id 再次重复,那么继续 for 循环直到 $i 小于 $num

但问题是它根本没有显示任何东西。我的意思是没有错误也没有结果!

这里出了什么问题,我该怎么办?

更新:

PRIVATE CODE

但结果又是:

【问题讨论】:

  • "$store_id = $sender_id; if($sender_id == $store_id){" -- 你看不出这段代码有什么问题吗?另外,关闭for 块的大括号丢失了,但我猜它是放在您发布的代码之后。
  • cant spot any problem 是什么意思?
  • $sender_id 什么时候不等于 $store_id?
  • 嗯,你可能不能,这就是你在这里的原因。我认为很明显,如果您在第一条语句中将$sender_id 的值复制到$store_id,它们是相等的,并且if 表达式在第二条语句中始终计算为TRUE

标签: php telegram telegram-bot php-telegram-bot


【解决方案1】:

问题是因为首先你要分配

$sender_id = $updateArray["result"][$i]["message"]["from"]["id"];

那么你正在分配

$store_id = $sender_id;

表示$store_id 将具有$sender_id 的确切内容

那么你正在检查

if($sender_id == $store_id)

这将永远是真的,并且每次循环都会得到continue

这就是为什么屏幕上没有显示任何内容,这不是语法错误。

您忘记在 $store_id 中分配正确的 store_id。

希望我能帮到你。祝你好运。

【讨论】:

  • 那么你认为什么是合理的而不是这个?
【解决方案2】:

问题是因为您只检查发件人是否已存在或其新发件人。如果它是新的,那么您将 sender_id 添加到数组并打印聊天框。如果它是一个已经存在的发件人,那么你什么都不做并继续。

表示您正在跳过为 id 已存在于数组中的发件人打印 msg。

所以现在不要继续使用 array_search() 像

$sender_ids = array();
for($i=0;$i<$num;$i++)
{
    $sender_id = $updateArray["result"][$i]["message"]["from"]["id"];
    if(!(in_array($sender_ids, $sender_ids)))
    {
        $sender_ids[] = $sender_id;
        echo ' CHAT BOX ';
    }
    else
    {
        $key = array_search($sender_ids, $sender_ids);
        // here this $key will be now same for all the messages from same sender id.
        // and you can make logic to group them.
    }
} 

希望我能帮到你。 :)

【讨论】:

    【解决方案3】:

    为什么要将 sender_id 与 store_id 进行比较。为了进行比较,它们应该来自不同的来源,然后您将检查它们是否相等。您应该实施一种方法来检查 sender_id 是否已经是他们的。您可以创建一个数组,然后继续将 sender_id 存储在数组中,但在分配之前,您将检查 sender_id 不应该已经存在于数组中。如果存在则继续。

    $sender_ids = array();
    
    for($i=0;$i<$num;$i++)
    {
        $sender_id = $updateArray["result"][$i]["message"]["from"]["id"];
        if(!(in_array($sender_ids, $sender_ids)))
        {
            $sender_ids[] = $sender_id;
            echo ' CHAT BOX ';
        }
        else
        {
            continue;
        }
    } 
    

    【讨论】:

    • 很好,但现在我得到了这些错误:Notice: Use of undefined constant haystack - assumed 'haystack' & in_array() expects parameter 2 to be array, string given
    • 我已经更新了代码。使用 if(!(in_array($sender_ids, $sender_ids))) 将数组名称放在 haystack 的位置,即 $sender_ids
    • 在 in_array() 中 haystack 的位置写入 $sender_ids。这将解决您的错误。
    • 嗯,Stack Overflow 的答案只是一个起点(或者只是指向正确的方向)。使用它来解决您当前的问题并进行研究以使其适应您的需求并改进您的代码。你不能指望我(或Stack Overflow 上的其他人)来修复你的代码。如果我这样做了,那它就不再是你的代码了,不是吗?
    • @GauravKumar 感谢您的努力,我更新了有关您的答案的问题,请参阅 UPDATE 2
    【解决方案4】:

    目前还不是很清楚你想要实现什么,但我想下面的代码可以做到:

    $updateArray = json_decode($update, TRUE);
    
    // Initialize $store_id with a value that doesn't appear in the input data
    $store_id = NULL;
    
    // foreach is better that for(;;) to iterate over collections
    foreach ($updateArray["result"] as $item) {
       // Get the sender of the current item
       $sender_id = $item["message"]["from"]["id"];
       // Use strict comparison to make sure it doesn't match when it shouldn't
       // (f.e. if `$sender_id` is the empty string it is == to NULL but not ===)
       if ($sender_id === $store_id) {
          // It is the same sender as of the previous message; skip this message
          continue;
       } else {
          // This is a new sender; process it...
          echo ' CHAT BOX ';
          // ... and remember it for the next items
          $store_id = $sender_id;
       }
    }
    

    【讨论】:

    • 更好的答案,但它显示了每条消息的所有框并且不介意if ($sender_id === $store_id) { // It is the same sender as of the previous message; skip this message continue; } 条件!!!
    • 我在更新的问题中发布了一张关于新结果的图片...查看一下。
    猜你喜欢
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多