I am a bit late to answer but writing to help someone who came here searching. Hopefully this will help someone.
// 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();
Complete script that will do is below down for copy
// 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]
);
}