代码之家  ›  专栏  ›  技术社区  ›  Pahoran Alanis

在数组中插入字符串变量

  •  0
  • Pahoran Alanis  · 技术社区  · 6 年前

    我在我的PHP项目中使用Sendinblue SMTP,我想将transactionals电子邮件发送到电子邮件的动态列表,问题是当我使用变量而不是字符串时出现语法错误。例如,这段代码工作得很好:

        include 'Mailin.php';
        $mailin = new Mailin('senders@sender.com', 'key');
        $mailin->
        addTo(
            array(
                     'email1@email.com' => '', 'email2@email.com' => '', 'email3@email.com' => ''
                )
        )->
    
        setFrom('sender@sender.com', 'Test')->
        setReplyTo('sender@sender.com', 'Test')->
        setSubject('Example')->
        setText('Test')->
        setHtml($htmlContent);
        $res = $mailin->send();
        print_r($res);
    

    但如果我使用变量代替“addTo Array”中的字符串,则会显示语法错误,例如:

        $customers = '';
        foreach ($clientes as $customer) {
    
            for ($i=1; $i < 41; $i++) { 
    
                if ($customer['email'.$i]  != "" or $customer['email'.$i] != NULL) {
    
                    $customers .= "'".$customer['email'.$i]. "' => '', " ; //for each customer's email add the email in " 'email@email.com' => '', " format
                }
            }
        }
    
        $customers = substr($customers, 0, -2); //removes last space and comma of the String
    
        include 'Mailin.php';
        $mailin = new Mailin('senders@sender.com', 'key');
        $mailin->
        addTo(
            array(
                     $customers
                )
        )->
    
        setFrom('sender@sender.com', 'Test')->
        setReplyTo('sender@sender.com', 'Test')->
        setSubject('Example')->
        setText('Test')->
        setHtml($htmlContent);
        $res = $mailin->send();
        print_r($res);
    

    如果我使用Print_r($customers)函数,它将显示我在第一个示例中使用的确切字符串,即使我使用代码:

        $text = "'email1@email.com' => '', 'email2@email.com' => '', 'email3@email.com' => ''";
    
        if ($customers == $text) {
            print_r("Yes");
        }else{
            print_r("No");
        }
    

    结果是“是”,但是当我在

        addTo(
            array(
                     $customers
                )
        )->
    

    显示错误,但如果我直接使用字符串,则会发送电子邮件

        addTo(
            array(
                     'email1@email.com' => '', 'email2@email.com' => '', 'email3@email.com' => ''
                )
        )->
    

    如果$customers变量有所需的字符串,我不知道为什么它会显示错误。

    你知道如何在我需要发送的电子邮件中使用变量吗?

    1 回复  |  直到 6 年前
        1
  •  3
  •   Barmar    6 年前

    不能通过将字符串与 => 在他们身上。要在关联数组中创建元素,只需指定给该数组索引。

    $customers = [];
    foreach ($customers as $customer) {
        for ($i = 1; $i < 41; $i++) {
            if (!empty($customer["email" . $i])) {
                $customers[$customer["email" . $i]] = "";
            }
        }
    }
    include 'Mailin.php';
    $mailin = new Mailin('senders@sender.com', 'key');
    $mailin->
    addTo($customers)->
    ...
    

    另外,请参见 Why non-equality check of one variable against many values always returns true? 为什么你要用 && 而不是 || 当你跳过空邮件时(我用 !empty() ).

    推荐文章