Code Snippet: Misc array counting function

by John Email

A friend wanted some simple code to count the number of times each email address in a list used the same domain.


// Our list of email addresses
$email_addresses = array("1@domain.com","asd2@domain.com","3@domain.com","4@blah.com","5@blah.com","6@whatever.com");

$domain_count = array();

// Loop through the email addres list counting the number of times each domain is found
foreach( $email_addresses as $key => $value )
{

// Find where the @ symbol is
$start = strpos($value, "@");

// Get the domain
$domain = substr($value, $start + 1, strlen($value));

// Add to the count
$domain_count[$domain] ++;

}

// Display the domain list with the count
foreach($domain_count as $key => $value )
{
echo "
$key was accessed $value times";
}

?>

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)