4

I'm trying to do a foreach loop inside a foreach loop.

I have a form where the user type in some text and hit send. On the server site I loop through an array with email addresses and send out the text message.

Now I also want the user to be able to use variables in the textarea, like $name. So my loop should first loop through the emails, and then str_replace the userinput $name variable, with the names in my array.

The loop works fine with the email part ($tlf), but not the replace $name part.

Could some spot what I am doing wrong?

$message = stripslashes(strip_tags($_POST['message2']));
$tlf=array("name1","name2");
$test=array("mname1", "mname2");
$subject = "Hello world";
$from = "[email protected]";
$headers = "From: $from";

foreach($tlf as $name){
    $to = $name. "@gmail.com";
    foreach($test as $navn){
        $message = str_replace('$navn', $navn, $message);}
    mail($to,$subject,$message,$headers);
}

Thanks a lot.

EDIT: The output is an email sent. Say the user type in "hello $name". I want it to first loop through the $tlf array, in this case creating 2 emails. This goes in as $to in the first loop. This works.

Now the next loop should recognize the user input "hello $name" and loop through the $test array replacing the user $name variable.

The output would be 2 emails send.

  1. Mail output: to: [email protected] message: hello mname1

  2. Mail output: to: [email protected] message: hello mname2

Let me know if I need to explain better, its hard for me to explain sorry.

5
  • In what way is the result wrong? Commented Jul 19, 2012 at 22:17
  • I forgot to mention that it does output the first name in the array "mname1", but it does it not loop through the array. It ouput $test[0] each time.. Commented Jul 19, 2012 at 22:19
  • Thanks for the reply Nate.. see my last comment. Commented Jul 19, 2012 at 22:20
  • where's the part that does output? Commented Jul 19, 2012 at 22:21
  • please tell us all your issues. It's really hard to diagnose a ghost. what do you expect and what do you get instead? Commented Jul 19, 2012 at 22:21

2 Answers 2

2

When you do the following:

str_replace('$navn', $navn, $message)

Then all literal occurences of $navn will be replaced (the first, the second, the third, ...). So running through that loop a second time can't possibly replace anything more.

You will need to define two placeholders, or make some distinction, or use preg_replace_callback if you want to declare in which order (or other logic) the possible replacement strings are applied.

If you had told us (but you haven't) you only wanted to replace the first occurence in each iteration, then a normal preg_replace(.., .., .., 1) would work.

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

1 Comment

Thanks a lot! I get it now. Will update my post as soon as I got it working.
1

Is this what you want?

$message = stripslashes(strip_tags($_POST['message2']));
$tlf=array(
            array("email" => "name1", "name" => "mname1"),
            array("email" => "name2", "name" => "mname2")
          );
$subject = "Hello world";
$from = "[email protected]";
$headers = "From: $from";

foreach($tlf as $contact){
    $to = $contact["email"] "@gmail.com";
    $replacedMessage = str_replace('$navn', $contact["name"], $message);
    mail($to,$subject,$replacedMessage,$headers);
}

4 Comments

Thanks for the reply. My problem is that the second foreach loop does not loop through the array. It simply outputs "mname1" each time. Does the second foreach loop "start over" each time, because its inside another loop?
Yes that looks very much like it. I just tested your code, didnt know I could do arrays in arrays :) - but it still only outputs "mname2" each time. EDIT: Checking it out
you were assigning it back to the same variable. I've updated it. try it now.
Thanks a lot! It works now, thats exactly what I wanted. I dont have my DB yet, but the point is to create the arrays from mysql db and then through this loop. Thanks a lot you helped me alot! Cheers

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.