如何在循环中进行循环(双循环)

时间:2017-05-28 10:19:49

标签: php arrays phpmailer

我有循环问题。

我有这样的代码,

foreach ($j as $empas) {


    date_default_timezone_set('Asia/Jakarta');
    $nomor = Nomor($i, $total);
    $proses = proses($total, $i) . "%";
    $timeServer = date("G:i:s");
    $toDay = date("d-M-Y");
    for($p=0;$p<$total;$p++){
        $user = $userlist[$p%count($userlist)] ;


        echo "\e[37m [" . $timeServer . "] - {$nomor}/{$total} - [{$proses}] {$user} > {$empas}  \e[0m ";;
        if (isEmail($empas)) {
            echo imakochan($empas, $letter, $host, $user, $pw, $port, $sendermail, $sendername, $subjek). "\n";
            } else {
        echo " This is not valid email ";
    }

    if ($i == $total) {
        echo "\n";
        echo " [0;34m ================================================================================== \n";
        echo " [0;34m [+]            Sending Proccess Completed on " . $timeServer . "            [+]\n";
    }
     $i++;
 } 
}

所以,我的问题是当php邮件发送电子邮件时,我无法旋转smtp。

我想通过电子邮件转动smtp,它应该通过电子邮件旋转

像这样

smtpuser1&gt; email1 - &gt;已发送!

smtpuser2&gt; email2 - &gt;发送!

smtpuser1&gt; email3 - &gt;发送!

smtpuser2&gt; email4 - &gt;发送!

当我这样做时,我遇到了问题:

[17:01:42] - 1/2 - [50%] smtpuser1 > focusedamos@hotmail.com   -> Sent!

[17:01:42] - 1/2 - [50%] smtpuser2 > focusedamos@hotmail.com   -> Sent!

  ==================================================================================
  [+]            Sending Proccess Completed on 17:01:42            [+]
 [17:01:50] - 3/2 - [150%] smtpuser1 > asowchizuru09@hotmail.com   -> Sent!
 [17:01:50] - 3/2 - [150%] smtpuser2 > asowchizuru09@hotmail.com   -> Sent!

1 个答案:

答案 0 :(得分:0)

不要为此做双循环。想想,当你使用双循环(循环内部循环)时,内部循环将在外部循环的每次运行中完全运行。

示例:

loop1 : run 5 times
  loop2: run 2 times

结果:

loop1 run1
  loop2 run1
  loop2 run2
loop1 run2
  loop2 run1
  loop2 run2
loop1 run3
  loop2 run1
  loop2 run2
loop1 run4
  loop2 run1
  loop2 run2
loop1 run5
  loop2 run1
  loop2 run2

要完成您的尝试,请使用更简单的方法。例如:

date_default_timezone_set('Asia/Jakarta');
$smtpArray = array(0 => 'smtp1.example.com', 1 => 'smtp2.example.com', 2 => 'smtp3.example.com');
$smtpCount = count($smtpArray);
$i = 0;
foreach ($j as $empas) {
    $nomor = Nomor($i, $total);
    $proses = proses($total, $i) . "%";
    $timeServer = date("G:i:s");
    $toDay = date("d-M-Y");
    $iteration = $i % $smtpCount; // Create a number of SMTP server to iterate it.
    $smtp = $smtpArray[$iteration]; // Use the SMTP server selected before.

    echo "\e[37m [" . $timeServer . "] - {$nomor}/{$total} - [{$proses}] {$smtp} > {$empas}  \e[0m ";
    if (isEmail($empas)) {
        echo imakochan($empas, $letter, $host, $smtp, $pw, $port, $sendermail, $sendername, $subjek). "\n";
    } else {
        echo " This is not valid email ";
    }

    if ($i == $total) {
        echo "\n";
        echo " [0;34m ================================================================================== \n";
        echo " [0;34m [+]            Sending Proccess Completed on " . $timeServer . "            [+]\n";
    }
    $i++; // Increase $i to use next SMTP server on next iteration.
}

我已经退出时区设置,因为你不需要设置循环的每次运行。