【问题标题】:how can i save array to another array in for loop?如何在 for 循环中将数组保存到另一个数组?
【发布时间】:2017-11-09 11:12:00
【问题描述】:

在 for 循环中我想使用发送消息方法(电报机器人)。

几次 ($arrlength),只有一次与文本(不是几次,总是 1 次)。

例如,这个 for 循环可能重复 20 次,我想发送 20 次消息和 1 次文本,但我上面的代码发送了几次带有文本的消息!然后我想我应该从 for 循环中取出我的发送消息方法并将所有索引数组保存在另一个数组中。

例如:这个 for 循环重复 3 次,我想将所有 3 个索引保存在另一个数组中,然后在 for 循环中使用发送消息方法

 //var : 
$saves =array();
$saves_id =array(); 
$saveus = array(); 

$resultt  = $conn->query("SELECT performer,file_id,title FROM databasebot 
 WHERE performer = '$message' or title = '$message'");

while ($row=mysqli_fetch_row($resultt))
{
// array title
      $saves[] = $row[2];
      //array file_id
      $saves_id[] = $row[1];


}
 $arrlength = count($saves);

   for($x = 0; $x <= $arrlength; $x++) {

    $poets['keyboard'][] = array($saves[$x]);

 $jsonPoets= json_encode($poets);
  //send message method ... !
 // chat_id , text , reply_markup are required in sendmessage method

 $url= "https://api.telegram.org/bot".$token."/sendMessage?
 chat_id=".$chat_id."&text= choose song : "."&reply_markup=".$jsonPoets;

   file_get_contents($url);
 }     

也许我可以这样做:

  for($x = 0; $x <= $arrlength; $x++) {

        $poets['keyboard'][] = array($saves[$x]);

// this in not working ... !
        $saveus[$x][] = $poets[$x] +$saveus[$x];        

}        

 $jsonPoets= json_encode($saveus);
$text= "song : ";
$url= "https://api.telegram.org/bot".$token."/sendMessage?
chat_id=".$chat_id."&text=".$text."&reply_markup=".$jsonPoets;
    file_get_contents($url);

我能做什么?

【问题讨论】:

    标签: php arrays for-loop telegram-bot


    【解决方案1】:

    根据我从您的问题中了解到的情况,您只想向 Telegram 发送一次消息,同时从多个 for 循环发送结果。

    试试这个代码:

    $poets = array(
    
        "keyboard" => array()
    
    );
    
    for($x = 0; $x <= $arrlength; $x++) {
    
    
        $poets['keyboard'][] = array($saves[$x]);
    
    
    }     
    
    $jsonPoets= json_encode($poets);
    //send message method ... !
    // chat_id , text , reply_markup are required in sendmessage method
    
    $url= "https://api.telegram.org/bot".$token."/sendMessage?
    chat_id=".$chat_id."&text= choose song : "."&reply_markup=".$jsonPoets;
    
    file_get_contents($url);
    

    【讨论】:

      猜你喜欢
      • 2014-04-30
      • 2018-04-13
      • 1970-01-01
      • 2021-07-28
      • 1970-01-01
      • 2018-06-26
      • 1970-01-01
      • 2021-09-16
      • 1970-01-01
      相关资源
      最近更新 更多