Multiple variables in foreach loop

How can I have more than one variable in a foreach loop?

I have tried this, however, it does not work.


foreach($names_array as $name && $emails_array as $email)

I would appreciate any help. Thanks.

Can you explain why you want to do this? Perhaps there could be an alternative method but before we can focus on that, it may be best to learn what you are trying to accomplish.

My guess would be … even if this was possible (haven’t tested it yet) but don’t think so, it would still take the same amount of time to process that foreach loop with two arrays to work with compared to having 2 foreach loops seperately.

Another idea would be to take your $names_array along with your $emails_array and put them in the same array to run just one foreach loop on it. :slight_smile: The array would be a little bigger, but would still be much faster than having your method loop through either two arrays at the same time and/or running 2 foreach loops.

Regards,
Peter

Well, I have an array of email addresses and an array of names. Both of the array are being retrieved from a mysql database. Now what I need to do is create a mail loop for each one, sending an email to everyone.


mail($name <$email>, $subject, message)

Thanks.

And how would you know which email belongs to which name ?

first you need to combine the arrays into one associative array

Assuming keys are the same (i.e. n-th email corresponds to n-th name), you should be able to write


foreach($names_array as $key => $name) {
   $email =  $emails_array[$key];

Most efficient would be to combine the 2 tables in your mysql statement using a JOIN.

The values in the names array correspond to the values in the emails array.