2017-05-28 211 views
0

我有循環問題。如何在循環中創建循環(雙循環)

我有這樣的代碼,

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++; 
} 
} 

所以,我的問題是我不能轉動SMTP當PHP郵件發送電子郵件。 >發送 -

我想SMTP電子郵件旋轉,應該通過電子郵件

這樣

smtpuser1> EMAIL1旋轉!

smtpuser2> email2 - >發送!

smtpuser1> email3 - >發送!

smtpuser2> email4 - >發送!

當我做這樣的,我有問題:

[17:01:42] - 1/2 - [50%] smtpuser1 > [email protected] -> Sent! 

[17:01:42] - 1/2 - [50%] smtpuser2 > [email protected] -> Sent! 

    ================================================================================== 
    [+]   Sending Proccess Completed on 17:01:42   [+] 
[17:01:50] - 3/2 - [150%] smtpuser1 > [email protected] -> Sent! 
[17:01:50] - 3/2 - [150%] smtpuser2 > [email protected] -> Sent! 

回答

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. 
} 

我已經走出時區設定的,因爲你並不需要設置循環的每次運行。

+0

$ user有什麼功能? – UmRchan

+0

我忘了刪除$ user變量。我用$ smtp變量替換它,對它們的使用更具描述性。 –

+0

在我的代碼中,我使用$ i = 1,但是你用它爲0.我怎麼改變它?因爲我使用它打印完整的橫幅。 – UmRchan