简体   繁体   中英

How to sort 2D array by key and value

I have lots of emails imported from txt file:

   $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. If usort is required can you give an example how to sort this?

Edit: I expect allowed chars in both levels are based on 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.

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

Then sort.

sort($emails);

Then group by 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.

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

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
        )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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