将字符串转换为数组用于sendinblue $sendSmtpEmail函数(PHP)

x6h2sr28  于 2023-01-01  发布在  PHP
关注(0)|答案(1)|浏览(124)

我从Mysql数据库中得到这个特定的字符串:

$string="john('john@yahoo.com'), frank('frank@gmail.com'),simon('simon@to.com')";

我需要在下面的代码中插入这个字符串:

$sendSmtpEmail = new \SendinBlue\Client\Model\SendSmtpEmail([
   'subject' => 'Report',
   'sender' => ['name' => 'sender', 'email' => 'sender@domain.com'],
   'to' => [
            ['name' => 'john', 'email' => 'john@domain.com'],
            ['name' => 'frank', 'email' => 'frank@domain.com'],
            ['name' => 'frank', 'email' => 'simon@domain.com']
           ],
   'htmlContent' => $output
]);

显然需要在$sendSmtpEmail中添加一个变量...但是如何创建呢?

2wnc66cl

2wnc66cl1#

我回答的有点晚,但是写信是为了帮助来这里寻找的人。希望这能帮助到一些人。

// String we are getting from the database
$string="john('john@yahoo.com'), frank('frank@gmail.com'),simon('simon@to.com')";
//First explode it with comma seprated to get an array from string like 
$string_exploded = explode(',', $string);
/*
array
(
    [0] => john('john@yahoo.com')
    [1] =>  frank('frank@gmail.com')
    [2] => simon('simon@to.com')
)
*/

// Loop each index to and than remove the last =>  ')   using str_replace() function to get value as  john('john@yahoo.com')  to john('john@yahoo.com
foreach($string_exploded as $singleIndex){ 
    // Loop each index to and than remove the last =>  ')   using str_replace() function to get value as  john('john@yahoo.com')  to john('john@yahoo.com
    $singleIndexParse = str_replace("')","", $singleIndex);
    // Again explode the each index string value with =>  ('  to get an array like 
    $arrayExplodedByBracket = explode("('",$singleIndexParse);
    /*
        array(
            [0] => john
            [1] => john@yahoo.com
        )
    */
    // Make an array with the name and email to pass for $to array 
    $to[] = array(
        "name"=>$arrayExplodedByBracket[0],
        "email"=>$arrayExplodedByBracket[1]
    );
}

    // Final You will get the $to array like 
    /*
    array
    (
        [0] => Array
            (
                [name] => john
                [email] => john@yahoo.com
            )

        [1] => Array
            (
                [name] =>  frank
                [email] => frank@gmail.com
            )

        [2] => Array
            (
                [name] => simon
                [email] => simon@to.com
            )

    )
    */
    //Print the array in pretty format
    echo "<pre>";
    print_r($to);
    echo "</pre>";
    die();

完整的脚本将在下面进行复制

// String we are getting from the database
$string="john('john@yahoo.com'), frank('frank@gmail.com'),simon('simon@to.com')";
//First explode it with comma seprated to get an array from string like 
$string_exploded = explode(',', $string);

// Loop each index to and than remove the last =>  ')   using str_replace() function to get value as  john('john@yahoo.com')  to john('john@yahoo.com
foreach($string_exploded as $singleIndex){ 
    // Loop each index to and than remove the last =>  ')   using str_replace() function to get value as  john('john@yahoo.com')  to john('john@yahoo.com
    $singleIndexParse = str_replace("')","", $singleIndex);
    // Again explode the each index string value with =>  ('  to get an array like 
    $arrayExplodedByBracket = explode("('",$singleIndexParse);

    // Make an array with the name and email to pass for $to array 
    $to[] = array(
        "name"=>$arrayExplodedByBracket[0],
        "email"=>$arrayExplodedByBracket[1]
    );
}

相关问题