我有一個電子郵件串列,如下所示:
[email protected]
[email protected]
[email protected]
123@hotmail.com
[email protected]
[email protected]
我想按擴展名對它們進行排序,然后將具有相同擴展名的電子郵件放在同一行:
[email protected]|123@hotmail.com
[email protected]|[email protected]|[email protected]
[email protected]
我的想法是使用 foreach() 和 strrev() 然后 sort() 反轉每一行,但我不知道如何將具有相同擴展名的電子郵件放在同一行:
foreach($emailLines as $eachLine){
$revArr[$i ] = strrev($eachLine);
}
sort($revArr);
請幫助我,謝謝。
uj5u.com熱心網友回復:
嘗試這個
<?php
$emails = [
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
];
$emailSort = [];
foreach ($emails as $email) {
$parts = explode('@', $email);
$emailSort[$parts[1]][] = $email;
}
ksort($emailSort);
$emailStrings = [];
foreach ($emailSort as $value) {
asort($value);
$emailStrings[] = implode('|', $value);
}
var_dump($emailStrings);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/489005.html
上一篇:用于插入和排序的最有效資料結構
