douzhun5971 2014-04-09 23:40
浏览 28
已采纳

PHP数组 - 按电子邮件地址域排序(备用)

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.

  • 写回答

1条回答 默认 最新

  • dongzhao1865 2014-04-10 07:23
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记