简体   繁体   English

如何按键和值对二维数组进行排序

[英]How to sort 2D array by key and value

I have lots of emails imported from txt file:我有很多从 txt 文件导入的电子邮件:

   $emails = file("exported_addresses.txt");
   $c = count($emails);
   for ( $i=0; $i<$c ; $i++ )
      $emails[$i] = strtolower(trim($emails[$i]));
   $portions = array();
   // $c = count($emails);
   for ( $i=0; $i<$c ; $i++ ):
     $sub = substr($emails[$i],0,2);
     if ( strlen($sub)==2 ) // e.g a1
       $sub .= $sub[0]." ";
     if ( !isset( $sub, $portions) ) 
       $portions[$sub] = array();
     $portions[$sub][] = $emails[$i];
   endfor;
   print_r($portions);die;

And I would like to sort the array in ascending order in both levels so this:我想在两个级别上按升序对数组进行排序,这样:

array( ['ma'] = array(
'martinu@yahoo.com',
'martina@post.com',
'marti@nette.com'),
['du'] = array(
'durkac@email.com',
'durek@net.com',
'dundy@gmail.com') )

woudl become this:会变成这样:

array(
['du'] = array(
'dundy@gmail.com',
'durek@net.com',
'durkac@email.com' ),
['ma'] = array(
'marti@nette.com',
'martina@post.com',
'martinu'@yahoo.com' )
 )

I could not find such example how to archieve this.我找不到这样的例子如何归档这个。 It is not clear to me if can I use array_multisort or do I need to write my own callback function.我不清楚是否可以使用 array_multisort 或者我是否需要编写自己的回调 function。 If usort is required can you give an example how to sort this?如果需要 usort,您能否举例说明如何对其进行排序?

Edit: I expect allowed chars in both levels are based on https://tools.ietf.org/html/rfc5322编辑:我希望两个级别中允许的字符都基于https://tools.ietf.org/html/rfc5322

ALPHA / DIGIT /    ; Printable US-ASCII
                       "!" / "#" /        ;  characters not including
                       "$" / "%" /        ;  specials.  Used for atoms.
                       "&" / "'" /
                       "*" / "+" /
                       "-" / "/" /
                       "=" / "?" /
                       "^" / "_" /
                       "`" / "{" /
                       "|" / "}" /
                       "~"

For my purposes I need only the two chars to be sorted.出于我的目的,我只需要对两个字符进行排序。

I'd suggest this:我建议这样做:

Do your strtolower / trim conversion in one step.一步完成您的strtolower / trim转换。

$emails = file("exported_addresses.txt");
foreach ($emails as $i => $email) {
    $emails[$i] = strtolower(trim($email));
}

Then sort.然后排序。

sort($emails);

Then group by substring.然后按 substring 分组。

$portions = [];
foreach ($emails as $email) {
    $portions[substr($email, 0, 2)][] = $email;
}

Since you already sorted before grouping, the groups and everything inside them will end up in the right order.由于您在分组之前已经进行了排序,因此组和其中的所有内容都将以正确的顺序结束。

First, we get the file content by using file_get_contents() like this.首先,我们像这样使用 file_get_contents() 获取文件内容。

$data = file_get_contents('./text.txt');

after this, we sort the array.在此之后,我们对数组进行排序。 ie array is:即数组是:

$data =array( 
'ma' => array(
'martinu@yahoo.com',
'martina@post.com',
'marti@nette.com'),
'du' => array(
'durkac@email.com',
'durek@net.com',
'dundy@gmail.com') );

**Solution:**

$c=[];

foreach ($a as $k=>$v) {
    sort($v);
    $c[$k]=$v;
}
ksort($c);

Output: Output:

Array
(
    du => Array
        (
            [0] => dundy@gmail.com
            [1] => durek@net.com
            [2] => durkac@email.com
        )

    ma => 

            [0] => marti@nette.com
            [1] => martina@post.com
            [2] => martinu@yahoo.com
        )

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM