0

I want to iterate array then It should be reset again in PHP . The following logic I want to implement

I have an array of smtp and a second array from which I just want to send email once per smtp . Actually I have a list of array for which I want to send email but I have few smtp hosts . I am iterating a list of array in foreach loop. I have a smtp array in which I have defined certain limit to send email.

function sendmail_test(){
   return "Sent<br/>";
}

$email_arrays=array(
   '[email protected]',
   '[email protected]',
   '[email protected]',
   '[email protected]',
   '[email protected]',
   '[email protected]',
);

$smtp_array=array(
   '[email protected]'=>10,
   '[email protected]'=>15
);


$smtp_count=count($smtp_array);
$smtp_counter=0;
for($i=0;$i<=$smtp_count;$i++){       

   foreach($email_arrays as $ek=>$ev){
        print_r($smtp_counter);
        echo sendmail_test();
    }
    $smtp_counter++;
}

Actually I want exactly like this. I have currently have two smtp in $smtp_array

First email should be fired by like this smtp [email protected] - >'[email protected]'

And second email should be fired by like this [email protected]',->'[email protected]'

and then third email should be fired like this [email protected] - >'[email protected]'

which Is then reset and will use first smtp in $smtp_array .. I hope you will get my point now.

4
  • 1
    your question is still unclear can you give us more details or where you are stuck now? Commented Jul 8, 2019 at 9:22
  • Is there a problem with the code you've posted? You state what you want but not the problem you are having. Commented Jul 8, 2019 at 9:27
  • Also $smtp_count is 2 and in the loop you check $i=0;$i<=$smtp_count which will loop 3 times for 0, 1 and 2. Is that intended? Commented Jul 8, 2019 at 9:31
  • I have updated my answer .Please see it Commented Jul 8, 2019 at 9:34

2 Answers 2

2

If you want to go though all the smtp servers and reset to the start when you had the last one, you might use current, end, reset, and next.

To get the value for the smtp, you could use key You can then add an smtp server to the list as well.

For example:

function sendmail_test()
{
    return "Sent<br/>";
}

$email_arrays = array(
    '[email protected]',
    '[email protected]',
    '[email protected]',
    '[email protected]',
    '[email protected]',
    '[email protected]',
);

$smtp_array = array(
    '[email protected]' => 10,
    '[email protected]' => 15
);

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

foreach ($email_arrays as $em) {
    $current = current($smtp_array);
    //sendmail_test();
    echo "Send email $em with smtp: " . key($smtp_array) . PHP_EOL;
    next($smtp_array);

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

Result:

Send email [email protected] with smtp: [email protected]
Send email [email protected] with smtp: [email protected]
Send email [email protected] with smtp: [email protected]
Send email [email protected] with smtp: [email protected]
Send email [email protected] with smtp: [email protected]
Send email [email protected] with smtp: [email protected]

Php demo

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much The fourth bird.You just solved my problem. !!
I am new to stackoverflow So I can't accept this as correct..but you did a good job,
@NarendraSharma - You can mark the question as accepted (the grey "v" mark at the left of the answer) - it is nothing to do with the reputation you have
0

You can approach this as following

$hosts = array_keys($smtp_array);
foreach($email_arrays as $k => $v){
  $email = $v;
  $host  = $hosts[$k%2];
  echo $email.'----'.$host;echo '<br/>';// use $email and $host to send the email
}

1 Comment

Thank you rakesh for commenting..I have updated my code. Please see my code again what I actually want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.