2016-07-12 69 views
0

我想通過域來洗牌電子郵件,使它們儘可能遠離彼此。例如:隨機播放和分離類似的字符串

陣列之前:

[email protected][email protected][email protected][email protected][email protected],EMAIL3 @的Gmail。 COM,[email protected][email protected]

它將成爲類似:

陣列後:

[email protected][email protected][email protected][email protected][email protected][email protected][email protected],email3 @ gmail .com

我該如何做一個數組?如果有任何PHP的例子,它會很好。

+0

你能告訴你有什麼到目前爲止已經試過? – mhatch

+0

這可能會幫助你開始:http://php.net/manual/en/array.sorting.php – Katie

回答

1
<?php 

$emails = array(
    '[email protected]', 
    '[email protected]', 
    '[email protected]', 
    '[email protected]', 
    '[email protected]', 

    '[email protected]', 
    '[email protected]', 
    '[email protected]', 
    '[email protected]', 
    '[email protected]', 

    '[email protected]', 
    '[email protected]', 
    '[email protected]', 
    '[email protected]', 
    '[email protected]', 

    '[email protected]', 
    '[email protected]', 
    '[email protected]', 
    '[email protected]', 
    '[email protected]', 

); 

$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)