有两个数组的foreach循环php中的条件有问题 [英] Having problem in conditions in foreach loop php with two arrays

查看:122
本文介绍了有两个数组的foreach循环php中的条件有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在foreach循环的数组中应用一些条件。实际上,我有两个数组,以后将使用它们从脚本发送电子邮件。因此,我的第一个数组包含smtp电子邮件ID。第二个包含所有电子邮件ID。例如

I want to apply some condition in arrays in foreach loop . Actually I have two arrays which I will use later to send emails from the script. So My first array is containing smtp email Ids. and the second one is containing all email ids. Such as

$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);
    }
}

我期望的输出是正确的但是,主要和麻烦在于,我对smtp电子邮件ID设置了限制,以发送从另一个smtp表中获取的电子邮件。

And I am having the output expected which is quite correct as per my requirement.But the main thing and trouble is I have set a limit on smtp email Id to send emails which I am getting from my another smtp table.

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

我已经成功建立了帐户但是最主要的是如果我对smtp1@gmail.com施加了限制(假设我已经施加了限制3),那么下一个smtp应该在数组中触发电子邮件,并且也应该存储电子邮件计数。任何帮助将不胜感激。

I have successfully Made the account I want to expected.But the main thing is if I have applied a limit on smtp1@gmail.com(suppose it I have applied a limit 3) then email should be fired by next smtp in array and should store the email counts too.. Any help will be greatly appreciated.

我想要此输出。.
如果我对smtp1@gmail.com设置3个限制,则只有3个电子邮件应被触发,然后迭代将继续使用数组中的另一个smtp

推荐答案

尝试一种略有不同的方法,使用第一次发送电子邮件直到达到每日限制,然后移至下一个:

Try an approach a little different, use first smpt to send email until the daily limit is reached, then move to the next one:

$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]++;

}

这篇关于有两个数组的foreach循环php中的条件有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆