0

I have an array($myArray)

Array ( [0] => 
          Array ( [0] => new
                  [1] => NFL930
                  [2] => Ohio
                  ...
                )
        [1] =>
          Array ( [0] => new
                  [1] => MLB382
                  [2] => Florida
                  ...
                )
        [2] =>
          Array ( [0] => new
                  [1] => MLB101
                  [2] => Vermont
                  ...
                )
        [3] =>
          Array ( [0] => new
                  [1] => NFL732
                  [2] => Alabama
                  ...
                )
        [4] =>
          Array ( [0] => old
                  [1] => MLB490
                  [2] => Texas
                  ...
                )
        [5] =>
          Array ( [0] => old
                  [1] => MLB821
                  [2] => Atlanta
                  ...
                )
        [6] =>
          Array ( [0] => old
                  [1] => NFL293
                  [2] => Maine
                  ...
                )
        .....
     )

I have a function that sorts the [0] index which is displayed above so all the "new" entries are first, then all of the "old" entries are listed.

usort($myArray, function($a, $b) use ($myValues){
    return $myValues[$a[0]] - $myValues[$b[0]];
  });

The array $myValues looks like

Array ( [New] => 0 [Old] => 1 [Other] => 2 )

I want to keep the [0] index sorting as is display all arrays with new first, then display array with old, etc etc. Then i want to display the ones with "NFL" before the ones with MLB. For example the desired output will be

Array ( [0] => 
      Array ( [0] => new
              [1] => NFL930
              [2] => Ohio
              ...
            )
    [1] =>
      Array ( [0] => new
              [1] => NFL732
              [2] => Alabama
              ...
            )
    [2] =>
      Array ( [0] => new
              [1] => MLB101
              [2] => Vermont
              ...
            )
    [3] =>
      Array ( [0] => new
              [1] => MLB382
              [2] => Florida
              ...
            )
    [4] =>
      Array ( [0] => old
              [1] => NFL293
              [2] => Maine
              ...
            )
    [5] =>
      Array ( [0] => old
              [1] => MLB821
              [2] => Atlanta
              ...
            )
    [6] =>
      Array ( [0] => old
              [1] => MLB490
              [2] => Texas
              ...
            )
    .....
 )
jumpman8947
  • 571
  • 1
  • 11
  • 34

1 Answers1

1

You can modify your usort:

usort($myArray, function($a, $b) use ($myValues){
   if ($myValues[$a[0]] - $myValues[$b[0]] == 0) {
       return strcmp($a[1],$b[1]);
   }
   return $myValues[$a[0]] - $myValues[$b[0]];
});

This will sort the entries according to index 1 if they're the same on index 0 (which means that NFL comes before MLB but also NFL001 comes before NFL002)

apokryfos
  • 38,771
  • 9
  • 70
  • 114