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