在foreach循环PHP和两个数组条件有问题(Having problem in conditio

2019-10-28 14:06发布

我想在foreach循环阵列应用一些条件。 其实我有,我会在以后使用该脚本发送电子邮件两个数组。 所以我的第一阵列包含SMTP电子邮件ID。 和第二个是包含所有电子邮件ID。 如

$email_arrays=$this->camp->get_contact_details(9);
$smtp_array=$this->camp->get_sender_details(10);

/*  pre($email_arrays);
pre($smtp_array); */

$lastElement = end($smtp_array);
reset($smtp_array);


foreach ($email_arrays as $em) {
    $current = current($smtp_array);
    $current_smtp_email=$current['email'];

    $dl=$current['daily_limit'];//current daily limit e.g.5 or 10 etc

    //sendmail_test();            
    echo "Send email ".$em['email']." with smtp: ".$current_smtp_email . " <br/>";


    next($smtp_array);            
    if ($lastElement === $current) {
        reset($smtp_array);
    }
}

而且我有输出预计这是按我的requirement.But最主要的相当正确的,麻烦的是我已经设置SMTP电子邮件ID的限送这我从我的另一个SMTP表中获取电子邮件。

Send email test1@example.com with smtp: smtp1@gmail.com
Send email test2@example.com with smtp: smtp2@gmail.com
Send email test3@example.com with smtp: smtp3@gmail.com
Send email test4@example.com with smtp: smtp1@gmail.com
Send email test5@example.com with smtp: smtp2@gmail.com
Send email test6@example.com with smtp: smtp3@gmail.com
Send email test7@example.com with smtp: smtp1@gmail.com
Send email test8@example.com with smtp: smtp2@gmail.com
Send email test9@example.com with smtp: smtp3@gmail.com
Send email test10@example.com with smtp: smtp1@gmail.com
Send email test11@example.com with smtp: smtp2@gmail.com
Send email test12@example.com with smtp: smtp3@gmail.com
Send email test13@example.com with smtp: smtp1@gmail.com
Send email test14@example.com with smtp: smtp2@gmail.com
Send email test15@example.com with smtp: smtp3@gmail.com

我已经成功地我想expected.But主要的是,如果我在阵列应用于smtp1@gmail.com(假设它我已经申请限制3)然后电子邮件应该在明年SMTP被解雇的限制的事项,并应存储电子邮件计数太..任何帮助将不胜感激。

我想这个输出..如果我上的3则只有3封电子邮件应该然后烧制smtp1@gmail.com申请限制迭代将继续使用在阵列另一个SMTP

Answer 1:

尝试的做法有点不同,先用SMPT发送邮件,直到达到涨停板,然后移动到下一个:

$lastElement = end($smtp_array);
reset($smtp_array);

$current = current($smtp_array);
$current_smtp_email=$current['email'];
$dl=$current['daily_limit'];//current daily limit e.g.5 or 10 etc
$send[$current_smtp_email] = 1;
foreach ($email_arrays as $em) {
    if ($send[$current_smtp_email] > $dl) {
        if ($lastElement === $current) {
            die ('No more SMTP to use today');
        }

    next($smtp_array);
    $current = current($smtp_array);
    $current_smtp_email=$current['email'];
    $dl=$current['daily_limit'];
    $send[$current_smtp_email] = 1;
}

echo "Send email ".$em['email']." with smtp: ".$current_smtp_email . " <br/>";
$send[$current_smtp_email]++;

}



文章来源: Having problem in conditions in foreach loop php with two arrays