php 解析逗号分隔的名称和电子邮件字符串以创建关联行数组

nhaq1z21  于 2023-01-04  发布在  PHP
关注(0)|答案(2)|浏览(93)

我从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中包含关联行的2d数组,但如何创建它呢?

a0zr77ik

a0zr77ik1#

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

// 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]
    );
}
mznpcxlj

mznpcxlj2#

使用preg_match_all()直接匹配一个或多个格式化子字符串序列。使用两个捕获组,然后将这些数据点Map到关联行。
代码:(Demo

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

preg_match_all("/(?:, ?)?([^(]+)\('([^']+)'\)/", $recipients, $matches, PREG_SET_ORDER);

$sendSmtpEmail = new \SendinBlue\Client\Model\SendSmtpEmail([
    'subject' => 'Report',
    'sender' => ['name' => 'sender', 'email' => 'sender@domain.com'],
    'to' => array_map(
        fn($row) => ['name' => $row[1], 'email' => $row[2]],
        $matches
    ),
    'htmlContent' => $output
]);

相关问题