简体   繁体   English

在foreach循环中的PHP foreach循环

[英]PHP foreach loop inside foreach loop

I'm trying to do a foreach loop inside a foreach loop. 我正在尝试在foreach循环中执行foreach循环。

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. 现在我也希望用户能够在textarea中使用变量,比如$ name。 So my loop should first loop through the emails, and then str_replace the userinput $name variable, with the names in my array. 所以我的循环首先应该遍历电子邮件,然后使用我的数组中的名称str_replace userinput $ name变量。

The loop works fine with the email part ($tlf), but not the replace $name part. 循环适用于电子邮件部分($ tlf),但不适用于替换$ name部分。

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 = "me@gmail.com";
$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". 假设用户输入“hello $ name”。 I want it to first loop through the $tlf array, in this case creating 2 emails. 我希望它首先遍历$ tlf数组,在这种情况下创建2封电子邮件。 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. 现在下一个循环应该识别用户输入“hello $ name”并循环遍历$ test数组,替换用户$ name变量。

The output would be 2 emails send. 输出将是2封电子邮件发送。

  1. Mail output: to: name1@gmail.com message: hello mname1 邮件输出:to:name1@gmail.com message:hello mname1

  2. Mail output: to: name1@gmail.com message: hello mname2 邮件输出:to:name1@gmail.com message:hello mname2

Let me know if I need to explain better, its hard for me to explain sorry. 让我知道如果我需要更好地解释,我很难解释对不起。

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, ...). 然后将替换$navn 所有文字出现(第一个,第二个,第三个,......)。 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. 如果要声明应用可能的替换字符串的顺序(或其他逻辑),则需要定义两个占位符或进行区分,或使用preg_replace_callback

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. 如果你告诉我们(但你没有)你只想在每次迭代中替换第一次出现,那么普通的preg_replace(.., .., .., 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 = "me@gmail.com";
$headers = "From: $from";

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM