mikeg542 Posted February 6, 2009 Share Posted February 6, 2009 Say I have a list of emails like so: $emails = array('[email protected]','[email protected]','[email protected]','[email protected]'); And want it sorted to be in the order: [email protected], [email protected], [email protected], [email protected]. My idea was to assign an unchangable value to the two parts of each (as in , explode them based on @, assign a value to each pair, sort by the second part then attach the first part back on based on the matching values) firstly, is this possible? and second, is it efficient for what I'm aiming for? Link to comment https://forums.phpfreaks.com/topic/144081-solved-sorting-an-array-of-emails-by-domain/ Share on other sites More sharing options...
omfgthezerg Posted February 6, 2009 Share Posted February 6, 2009 use sort() http://www.php.net/sort Link to comment https://forums.phpfreaks.com/topic/144081-solved-sorting-an-array-of-emails-by-domain/#findComment-755982 Share on other sites More sharing options...
mikeg542 Posted February 6, 2009 Author Share Posted February 6, 2009 How would I use sort to only do it for the domain? I want: [email protected] to come before [email protected] Link to comment https://forums.phpfreaks.com/topic/144081-solved-sorting-an-array-of-emails-by-domain/#findComment-755987 Share on other sites More sharing options...
Mark Baker Posted February 6, 2009 Share Posted February 6, 2009 $emails = array('[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]','[email protected]'); echo 'Unsorted<br />'; print_r($emails); echo '<br />'; function domainSort($a, $b) { list($aMailbox,$aDomain) = explode('@',$a); list($bMailbox,$bDomain) = explode('@',$b); if ($aDomain == $bDomain) { return ($aMailbox < $bMailbox) ? -1 : 1; } return ($aDomain < $bDomain) ? -1 : 1; } usort($emails,'domainSort'); echo 'Sorted<br />'; print_r($emails); Link to comment https://forums.phpfreaks.com/topic/144081-solved-sorting-an-array-of-emails-by-domain/#findComment-755997 Share on other sites More sharing options...
mikeg542 Posted February 6, 2009 Author Share Posted February 6, 2009 Thank you very much! Link to comment https://forums.phpfreaks.com/topic/144081-solved-sorting-an-array-of-emails-by-domain/#findComment-756023 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.