Home > Mobile >  Saving results of foreach loops
Saving results of foreach loops

Time:01-10

I have one raw like [This]

It was creating a mess so I decided to create by own. The code I was able to write -

<?php

if (preg_match_all('/\btest\b/', $message)) {
    $sendmes = "https://api.telegram.org/bot".$botToken."/sendMessage?chat_id=".$chatId."&text=<code>Wait...</code>&reply_to_message_id=".$message_id."&parse_mode=HTML";
    $sent = json_decode(file_get_contents($sendmes) ,1);
    $mes_id = $sent['result']['message_id'];
    $cclist = preg_replace("/[^0-9|\n]/", "",$message);
    $array = explode("\n", $cclist);
    foreach ($array as $res) {
        editMessage($chatId,"CHECKING CC : $res",$mes_id);
        $cc = file_get_contents('https://oneclass.junaidrahman2.repl.co/***.php?lista='.$res.'');
        $msg = trim(strip_tags(getStr($cc,' <br>Result:','</span><br>')));
        $i = "CC : $res
Result : $msg";
       editMessage($chatId,"$i",$mes_id);
  }
}

it will edit the message Everytime. I need to save the results of the previous loop. Like :

CC : 72611111

Result : Abc 

CC : 625262

Result : jsh

I don't know how I can do it. Please help

CodePudding user response:

You should structure your foreach like so:

if (preg_match_all('/\btest\b/', $message)) {
        $sendmes = "https://api.telegram.org/bot".$botToken."/sendMessage?chat_id=".$chatId."&text=<code>Wait...</code>&reply_to_message_id=".$message_id."&parse_mode=HTML";
        $sent = json_decode(file_get_contents($sendmes) ,1);
        $mes_id = $sent['result']['message_id'];
        $cclist = preg_replace("/[^0-9|\n]/", "",$message);
        $array = explode("\n", $cclist);
        //Initialize an empty array here to collect the item in the loop
         $new_array = [];
        foreach ($array as $res) {
            editMessage($chatId,"CHECKING CC : $res",$mes_id);
            $cc = file_get_contents('https://oneclass.junaidrahman2.repl.co/***.php?lista='.$res.'');
            $msg = trim(strip_tags(getStr($cc,' <br>Result:','</span><br>')));
            $i = "CC : $res
Result : $msg";
           editMessage($chatId,"$i",$mes_id);
          // Then inside your foreach your item get pushed into the array
          //on each iteration like so:
          $new_array[] = $i
      }
    }

You can see the value of $new_array after the foreach loop by printing it like so:

print_r($new_array);

  • Related