PHP Arrays - Sort by Email Address Domain (alternate)

danielb

I have an array of email addresses I am sending emails to.

I would like to sort them by alternating domain names, so if I have 30 @gmail.com, 30 @yahoo.com and 30 @aol.com, the sort would result in a @gmail.com, then @yahoo.com, then @aol.com, then @gmail.com again, etc.

The sort would alternate as much as possible so that there would be as few identical domain names in a row.

Why: To prevent being considered as a source of spam, its best to "throttle" email sending, or sleep between each send so mail servers are not hit quickly many times in a short time spam. Instead, I would like to do that above to create a lag between times an email provider is hit by me, but without stopping my script and causing a delay to my end user.

user1978142

I may do it like this:

$organized_emails = array();
$needle_key = 0;
$needle_search = array('gmail', 'yahoo', 'aol', 'others');

while(true) {
    $current_value = array_shift($emails);
    if(strpos($current_value, $needle_search[$needle_key]) !== false) {
        $organized_emails[] = $current_value;
        $needle_key++;
        if($needle_key > 3) {
            $needle_key = 0;
        }
    } else {
        array_push($emails, $current_value);
    }

    if(empty($emails)) {
        break;
    }
}

PHP Fiddle sample

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP, Validate if user entered an email domain, NOT address

From Dev

Alternate Email Address each time a form submits

From Dev

php email domain name

From Dev

PHP: Sort emails by domain

From Dev

PHP: Sort emails by domain

From Dev

How to make preg_match PHP function match domain in email address format?

From Dev

Require Domain on Email Address jQuery Validation

From Dev

Email Address with atleast 2 Character domain extension

From Dev

Are comments allowed in email address domain part?

From Dev

Ruby Regex to extract domain from email address

From Dev

Jquery validation for email address or domain name

From Dev

Ruby Regex to extract domain from email address

From Dev

Sending email from domain name address

From Dev

Spark: Extaract domain from email address in dataframe

From Dev

Sort and echo data from array by email domain

From Dev

Sort and echo data from array by email domain

From Dev

How to setup domain email address forwarding to my private email?

From Dev

IPv6 address as the domain portion of an email address

From Dev

Linux bash SORT by last name in email address

From Dev

PHP mail is not sending email to my email address

From Dev

PHP mail is not sending email to my email address

From Dev

Partially hide email address in PHP

From Dev

Is a valid paypal email address or not (PHP)

From Dev

Hide part of email address in php

From Dev

Parse email sent to an address with PHP?

From Dev

PHP sort multiple arrays

From Dev

PHP sort database by domain name

From Dev

From email address find the user in multi-domain AD environment

From Dev

RegEx match only final domain name from any email address

Related Related

HotTag

Archive