我正在使用用PHP编写的CRM,我需要创建一组高度使用的电子邮件地址,例如:
emailFirst@domain.com
emailThird@domain.com
emailThird@domain.com
emailScond@domain.com
emailThird@domain.com
emailScond@domain.com
这里是我想要的:
emailThird@domain.com (3)
emailScond@domain.com (2)
emailFirst@domain.com (1)
我尝试使用php中的代码:
$string_emails = "emailFirst@domain.com emailThird@domain.com emailThird@domain.com
emailScond@domain.com emailThird@domain.com emailScond@domain.com";
$emails = str_word_count($string_emails, 1);
$ret = array_count_values($emails );
print_r($ret);
但是没有运气,它只是向我展示。
感谢。
答案 0 :(得分:1)
$string_emails = "emailFirst@domain.com emailThird@domain.com
emailThird@domain.com emailScond@domain.com emailThird@domain.com
emailScond@domain.com";
// we create an array with the values if you really want to have an str for your values
$emails = explode( ' ', $string_emails );
$emails_list = array();
foreach($emails as $item)
{
$emails_list[$item] = $emails_list[$item]+1;
}
print_r($emails_list);
答案 1 :(得分:0)
$string_emails = "emailFirst@domain.com emailThird@domain.com
emailThird@domain.com emailScond@domain.com emailThird@domain.com
emailScond@domain.com";
$emails = explode($string_emails, ' ');
print_r(array_count_values($emails));
输出:
Array (
[emailFirst@domain.com] => 1
[emailThird@domain.com] => 3
[emailScond@domain.com] => 2 )
答案 2 :(得分:0)
这个怎么样
$emails = "emailFirst@domain.com emailThird@domain.com emailThird@domain.com
emailScond@domain.com emailThird@domain.com emailScond@domain.com";
$array = array();
$array = explode(" ",$emails);
if(sizeof($array) > 0 ){
$final_array = array();
foreach($array as $key=>$val){
$final_array[$val] += 1 ;
}
}
print_r($final_array);
输出::
Array ( [emailFirst@domain.com] => 1 [emailThird@domain.com] => 3 [ emailScond@domain.com] => 1 [emailScond@domain.com] => 1 )
<强>更新强>
发现问题,额外的空间可能会导致问题,因此此行将解决此问题
$final_array[trim($val)] += 1 ;
而不是
$final_array[$val] += 1 ;