du9698 2016-07-12 14:13
浏览 35

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

I would like to shuffle emails by domains, making them as far as possible from each other. Example:

Array before:

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

It would become something like:

Array after:

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

How can I do it with an Array? If there is any example with PHP it would be great.

  • 写回答

1条回答 默认 最新

  • doudian7996 2016-07-12 14:32
    关注
    <?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>';
    

    Sorry, I just found what I need at PHP Arrays - Sort by Email Address Domain (alternate)

    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等