繁体   English   中英

如何从PHP数组中的JSON提取电子邮件?

[英]How to extract the emails from the JSON in the PHP array?

我如何从该阵列获取电子邮件?

array(16) { 
    [0]=> string(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},"
}

看起来该数组中的每个字符串都是JSON数据。

如果您使用的是现代版本的PHP,则可以使用json_decode()将数据转换为可用格式。

foreach($array as $string) {
    $json = json_decode($string);
    echo "Email = {$json->email}\n";
}

如果您可以发布数据示例(例如:数据来自何处,数组的print_r()输出的格式正确的示例)会有所帮助,但是从我可以收集的信息中,这将从数组中获取电子邮件:

/* Make $array hold the given array */

$emails = array();
foreach($array as $contact){
    $emails[] = $contact['email'];
}

// All emails
print_r($emails);

您可以在每个数组元素上运行一个正则表达式。 像这样的东西:/“电子邮件”:“(。+?)” /

$emails = array();
foreach ($array as $str)
{
    if (preg_match('/"email":"(.+?)"/', $str, $matches))
    {
        $emails[] = $matches[1];
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM