0

I have an array($myArray)

[
    ['new', 'NFL732', 'Alabama'],
    ['new', 'NFL930', 'Ohio'],
    ['old', 'MLB490', 'Texas'],
    ['new', 'MLB101', 'Vermont'],
    ['old', 'MLB821', 'Atlanta'],
    ['old', 'NFL293', 'Maine'],
    ['new', 'MLB382', 'Florida'],
]

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
              ...
            )
    .....
 )
1
  • 1
    customizing your user sort function should do the trick. Commented Sep 7, 2017 at 14:42

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

0

You can custom sort by the first column then by the prefix of the second column.

array_multisort(): (Demo)

$priority0 = array_flip(['new', 'old', 'other']);
$priority1 = array_flip(['NFL', 'MLB']);
array_multisort(
    array_map(
        fn($row) => $priority0[$row[0]] ?? PHP_EOL,
        $array
    ),
    array_map(
        fn($row) => $priority1[rtrim($row[1], '0..9')] ?? PHP_EOL,
        $array
    ),
    $array
);
var_export($array);

Or usort(): (Demo)

$priority0 = array_flip(['new', 'old', 'other']);
$priority1 = array_flip(['NFL', 'MLB']);
usort(
    $array,
    fn($a, $b) => ($priority0[$a[0]] ?? PHP_EOL) <=> ($priority0[$b[0]] ?? PHP_EOL)
        ?: ($priority1[rtrim($a[1], '0..9')] ?? PHP_EOL) <=> ($priority1[rtrim($b[1], '0..9')] ?? PHP_EOL)
);
var_export($array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.