在php中将字符串拆分为数组对象

时间:2021-08-30 21:39:58

I have a string like this: "abc@gmail.com;ABC,xyz@gmail.com;XYZ" and I want to convert into array boject. How can I do that? Below is sample

我有一个这样的字符串:“abc@gmail.com; ABC,xyz @ gmail.com; XYZ”,我想转换成数组boject。我怎样才能做到这一点?以下是样本

$noactivity_noassignedto = "abc@gmail.com;ABC,xyz@gmail.com;XYZ";
$assignedto = explode(';', $noactivity_noassignedto);
$assignedto = array((object) array("Email" => $assignedto[0], "Name" => $assignedto[1]));
$fromadd = 'some@abc.com';
$fromname = 'somename';
/*sending mail here*/
$this->init()->setsubject($sub)->addto($assignedto)->setfrom($fromadd, $fromname)->send();

5 个解决方案

#1


1  

Try this code:

试试这段代码:

$noactivity_noassignedto = "abc@gmail.com;ABC,xyz@gmail.com;XYZ";
$assignedto = explode(',', $noactivity_noassignedto);

foreach ($assignedto as $recipient) {
    $tmp = explode(';', $recipient);
    $recipients[] = (object)array("Email" => $tmp[0], "Name" => $tmp[1]);
}
$recipients = (object)$recipients;

#2


1  

$noactivity_noassignedto = "abc@gmail.com;ABC,xyz@gmail.com;XYZ";
$elements = explode(',', $noactivity_noassignedto);
foreach ($elements as $element) {
    $dummy = explode(';', $element);
    $assignedto[] = (object)array("Email" => $dummy[0], "Name" => $dummy[1]);
}

#3


0  

You need two explode

你需要两次爆炸

   $noactivity_noassignedto = "abc@gmail.com;ABC,xyz@gmail.com;XYZ";
   $assignedAll = explode(',', $noactivity_noassignedto);

   // here you can put loop to send all, for now just first
   $assignedto = explode(';', $assignedAll[0]);
   $assignedto = array((object) array("Email" => $assignedto[0], "Name" => $assignedto[1]));

#4


0  

This will generate the array of objects that you want

这将生成您想要的对象数组

$noactivity_noassignedto = "abc@gmail.com;ABC,xyz@gmail.com;XYZ";
$assignedto = explode(',', $noactivity_noassignedto);

foreach($assignedto as $item) {
    list($email, $name) = explode(';', $item);
    $addresses[] = (object)['Name'=>$name, 'Email'=>$email];
}

print_r($addresses);

Result: a print_r($addresses);

结果:print_r($ addresses);

Array
(
    [0] => stdClass Object
        (
            [Name] => ABC
            [Email] => abc@gmail.com
        )

    [1] => stdClass Object
        (
            [Name] => XYZ
            [Email] => xyz@gmail.com
        )

)

#5


-1  

You are on the right path. explode is what you are looking for to split a string into an array. Given

你是在正确的道路上。 explode是你想要将字符串拆分成数组的东西。特定

$str = "abc@gmail.com;ABC";
$arr = explode($str, ';');

$arr will be array('abc@gmail.com', 'ABC');.

$ arr将是数组('abc @ gmail.com','ABC');.

I think your problem is you want to invoke this multiple times. To do this:

我认为你的问题是你要多次调用它。去做这个:

$str = "abc@gmail.com;ABC,def@gmail.com;DEF";
$people = explode($str, ',');
foreach ($people as $person) {
    $assigned_to = explode($person, ';');
    // rest of your code
}

#1


1  

Try this code:

试试这段代码:

$noactivity_noassignedto = "abc@gmail.com;ABC,xyz@gmail.com;XYZ";
$assignedto = explode(',', $noactivity_noassignedto);

foreach ($assignedto as $recipient) {
    $tmp = explode(';', $recipient);
    $recipients[] = (object)array("Email" => $tmp[0], "Name" => $tmp[1]);
}
$recipients = (object)$recipients;

#2


1  

$noactivity_noassignedto = "abc@gmail.com;ABC,xyz@gmail.com;XYZ";
$elements = explode(',', $noactivity_noassignedto);
foreach ($elements as $element) {
    $dummy = explode(';', $element);
    $assignedto[] = (object)array("Email" => $dummy[0], "Name" => $dummy[1]);
}

#3


0  

You need two explode

你需要两次爆炸

   $noactivity_noassignedto = "abc@gmail.com;ABC,xyz@gmail.com;XYZ";
   $assignedAll = explode(',', $noactivity_noassignedto);

   // here you can put loop to send all, for now just first
   $assignedto = explode(';', $assignedAll[0]);
   $assignedto = array((object) array("Email" => $assignedto[0], "Name" => $assignedto[1]));

#4


0  

This will generate the array of objects that you want

这将生成您想要的对象数组

$noactivity_noassignedto = "abc@gmail.com;ABC,xyz@gmail.com;XYZ";
$assignedto = explode(',', $noactivity_noassignedto);

foreach($assignedto as $item) {
    list($email, $name) = explode(';', $item);
    $addresses[] = (object)['Name'=>$name, 'Email'=>$email];
}

print_r($addresses);

Result: a print_r($addresses);

结果:print_r($ addresses);

Array
(
    [0] => stdClass Object
        (
            [Name] => ABC
            [Email] => abc@gmail.com
        )

    [1] => stdClass Object
        (
            [Name] => XYZ
            [Email] => xyz@gmail.com
        )

)

#5


-1  

You are on the right path. explode is what you are looking for to split a string into an array. Given

你是在正确的道路上。 explode是你想要将字符串拆分成数组的东西。特定

$str = "abc@gmail.com;ABC";
$arr = explode($str, ';');

$arr will be array('abc@gmail.com', 'ABC');.

$ arr将是数组('abc @ gmail.com','ABC');.

I think your problem is you want to invoke this multiple times. To do this:

我认为你的问题是你要多次调用它。去做这个:

$str = "abc@gmail.com;ABC,def@gmail.com;DEF";
$people = explode($str, ',');
foreach ($people as $person) {
    $assigned_to = explode($person, ';');
    // rest of your code
}