AdRock Posted March 19, 2009 Share Posted March 19, 2009 When i connect to the database I create 2 arrays while ($row = $result->fetch()) { $emails[] = $row['email']; $names[] = ucwords($row['first_name']); } and i want to use both arrays in my foreach loop at the moment if does the foreach using the email array but it doesn't do the foreach for the names array so it applies the same name to each of the emails instead of goping through each email address and getting each name foreach($emails as $email) { $to = $email; $HTML = str_replace($nameholder, $names, $HTML); $HTML = str_replace($emailholder, $email, $HTML); $result = sendHTMLemail($HTML,$from,$to,$subject); if($result != 'Correct') { return "Emails not sent"; } } How can i get each name in the array to match each email? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted March 19, 2009 Share Posted March 19, 2009 you can do it like this: foreach($emails as $n=>$email) { $name = $names[$n]; $to = $email; $HTML = str_replace($nameholder, $names, $HTML); $HTML = str_replace($emailholder, $email, $HTML); $result = sendHTMLemail($HTML,$from,$to,$subject); if($result != 'Correct') { return "Emails not sent"; } } but is there a reason you don't just use a multidimensional array? $data = array(); while ($row = $result->fetch()) { $data[] = array( 'email' => $row['email'], 'name' => ucwords($row['first_name']) ); } foreach($data as $item) { $to = $item['email']; $HTML = str_replace($nameholder, $item['name'], $HTML); $HTML = str_replace($emailholder, $item['email'], $HTML); $result = sendHTMLemail($HTML,$from,$to,$subject); if($result != 'Correct') { return "Emails not sent"; } } Quote Link to comment Share on other sites More sharing options...
AdRock Posted March 19, 2009 Author Share Posted March 19, 2009 Thanks rhodesa I was thinking about a 2D array but never had to use it before. The second example is what i was trying to get as a solution Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.