How to extract the emails from the JSON in the PHP array?
How can I get the emails from this array?
array(16) {
[0]=> stri开发者_JAVA百科ng(273) ""guid":"","contactId":"44","contactName":"_, atri","email":"atri_megrez@yahoo.com","emaillink":"http:\/\/mrd.mail.yahoo.com\/compose?To=atri_megrez%40yahoo.com","isConnection":false,"connection":"","displayImg":null,"msgrID":"atri_megrez","msgrStatus":"","isMsgrBuddy":122},"
[1]=> string(260) ""guid":"","contactId":"100","contactName":"afrin","email":"fida_cuty123@yahoo.com","emaillink":"http:\/\/mrd.mail.yahoo.com\/compose?To=fida_cuty123%40yahoo.com","isConnection":false,"connection":"","displayImg":null,"msgrID":"","msgrStatus":"","isMsgrBuddy":false},"
[2]=> string(258) ""guid":"","contactId":"101","contactName":"afrin","email":"waliyani@yahoo.com","emaillink":"http:\/\/mrd.mail.yahoo.com\/compose?To=waliyani%40yahoo.com","isConnection":false,"connection":"","displayImg":null,"msgrID":"","msgrStatus":"","isMsgrBuddy":false},"
}
It looks like each of the strings in that array is JSON data.
If you're using a modern version of PHP, you can use json_decode()
to get to the data into a usable format.
foreach($array as $string) {
$json = json_decode($string);
echo "Email = {$json->email}\n";
}
If you could post an example of the data (eg: where it comes from, a properly formatted example of the print_r()
output of the array) that would help, however from what I can gather this will get the emails from the array:
/* Make $array hold the given array */
$emails = array();
foreach($array as $contact){
$emails[] = $contact['email'];
}
// All emails
print_r($emails);
you can run a regexp on every array element. something like this: /"email":"(.+?)"/
$emails = array();
foreach ($array as $str)
{
if (preg_match('/"email":"(.+?)"/', $str, $matches))
{
$emails[] = $matches[1];
}
}
精彩评论