随机播放和分开相似的字符串

时间:2016-07-12 14:13:25

标签: php sorting

我想通过域名对电子邮件进行随机播放,使它们尽可能地相互隔离。例如:

之前的数组:

  

email @ hotmail.com,email2 @ hotmail.com,email3 @ hotmail.com,   email @ gmail.com,email2 @ gmail.com,email3 @ gmail.com,email @ yahoo.com,   email2@yahoo.com

它会变成:

之后的数组:

  

email @ hotmail.com,email @ gmail.com,email @ yahoo.com,   email2 @ hotmail.com,email2 @ gmail.com,email2 @ yahoo.com,   email3 @ hotmail.com,email3 @ gmail.com

如何使用数组?如果有任何PHP示例,那就太棒了。

1 个答案:

答案 0 :(得分:1)

<?php

$emails = array(
    'mail1@gmail.com',
    'mail2@gmail.com',
    'mail3@gmail.com',
    'mail4@gmail.com',
    'mail5@gmail.com',

    'mail1@yahoo.com',
    'mail2@yahoo.com',
    'mail3@yahoo.com',
    'mail4@yahoo.com',
    'mail5@yahoo.com',

    'mail1@aol.com',
    'mail2@aol.com',
    'mail3@aol.com',
    'mail4@aol.com',
    'mail5@aol.com',

    'mail1@others.com',
    'mail2@others.com',
    'mail3@others.com',
    'mail4@others.com',
    'mail5@others.com',

);

$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;
    }
}

echo '<pre>';
print_r($organized_emails);
echo '</pre>';

抱歉,我刚刚在PHP Arrays - Sort by Email Address Domain (alternate)

找到了我需要的内容