2016-07-12 2 views
0

Ich möchte E-Mails nach Domänen mischen, so dass sie so weit wie möglich voneinander entfernt. Beispiel:Mischen und trennen ähnliche Zeichenfolgen

Array vor:

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

Es wäre so etwas wie sein:

Array nach:

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

Wie kann ich es mit einem Array tun? Wenn es ein Beispiel mit PHP gibt, wäre es großartig.

+0

Können Sie zeigen, was Sie bisher versucht haben? – mhatch

+0

Dies könnte Ihnen den Einstieg erleichtern: http://php.net/manual/en/array.sorting.php – Katie

Antwort

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>'; 

Sorry, fand ich genau das, was ich bei PHP Arrays - Sort by Email Address Domain (alternate) brauchen