0

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://www.rfc-editor.org/rfc/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.

Community
  • 1
  • 1
Johny Bony
  • 375
  • 2
  • 9
  • I don't see how these questions are identical. This is completly different array. The array here https://stackoverflow.com/questions/2699086/how-to-sort-multi-dimensional-array-by-value should keep the 2nd level of order of keys untouched. – Johny Bony Oct 18 '19 at 17:37
  • I'm unclear on the sorting criteria for the items in the 'ma' portion. Can you explain a bit why the goal result is that order? Are you wanting to just ignore any non-alpha characters? – Don't Panic Oct 18 '19 at 17:43
  • 'ma' portion corrected. Sorry for typo. Just normal ascending order in both levels. The first level by key, the second level by value. Is it possible to sort the emails by all the allowed chars? – Johny Bony Oct 18 '19 at 17:49
  • Respectfully disagree about the duplicate closure. I agree with Johny that this isn't asking the same thing as that Q. – Don't Panic Oct 18 '19 at 18:03

2 Answers2

2

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. i.e. 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
        )

)
Mohit Sidoliya
  • 164
  • 1
  • 6
1

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.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • It seems like sort is kind of clever function which can recognise automatically that its content are arrays, so the only way to sort them is basicly to perform ksort... – Johny Bony Oct 18 '19 at 19:55