Code Snippet: Misc array counting function
09/08/2009
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”;
}
?>
This entry was posted in Computers and tagged PHP. Bookmark the permalink.
← Nokia: Customer Service – Update Code Snippet: Photo Randomiser →
Leave a Reply
You must be logged in to post a comment.







