I am currently working with arrays and sorting. I have worked with the usual traditional ways to organize an array. However, I also just found out about uasort()
and how it allows sorting of an array with a user-defined comparison function and maintains index association. What would be the best way to apply uasort
and sort the below array based on a user define order?
Desired order Principal, Teacher, Security, BusDriver, Maintanance
:
array(8) {
[0]=>
object(stdClass)#23019 (9) {
["id"]=>
int(1599874)
["firstName"]=>
string(3) "Jim"
["lastName"]=>
string(6) "Turner"
["position"]=>
string(9) "Principal"
}
[1]=>
object(stdClass)#23034 (9) {
["id"]=>
int(14398838)
["firstName"]=>
string(7) "Michael"
["lastName"]=>
string(6) "Watson"
["position"]=>
string(9) "Principal"
}
[2]=>
object(stdClass)#23038 (9) {
["id"]=>
int(17398661)
["firstName"]=>
string(8) "Jermaine"
["lastName"]=>
string(4) "Shaw"
["position"]=>
string(7) "Teacher"
}
[3]=>
object(stdClass)#23003 (9) {
["id"]=>
int(17398683)
["firstName"]=>
string(5) "Maria"
["lastName"]=>
string(6) "Garcia"
["position"]=>
string(7) "Teacher"
}
[4]=>
object(stdClass)#23020 (9) {
["id"]=>
int(17398686)
["firstName"]=>
string(5) "Jacob"
["lastName"]=>
string(5) "Smith"
["position"]=>
string(7) "Teacher"
}
[5]=>
object(stdClass)#23021 (9) {
["id"]=>
int(22398843)
["firstName"]=>
string(4) "Alex"
["lastName"]=>
string(4) "Hood"
["positionUsed"]=>
string(8) "Security"
}
[6]=>
object(stdClass)#23036 (9) {
["id"]=>
int(13398706)
["firstName"]=>
string(4) "Jose"
["lastName"]=>
string(8) "Bautista"
["position"]=>
string(9) "BusDriver"
}
[7]=>
object(stdClass)#23029 (9) {
["id"]=>
int(12398646)
["firstName"]=>
string(6) "Travis"
["lastName"]=>
string(9) "Dickerson"
["position"]=>
string(11) "Maintenance"
}
}
Code:
$staff1 = [
[
'id' => 1599874,
'firstName' => 'Jim',
'lastName' => 'Turner',
'position' => 'Principal',
],
[
'id' => 12398646,
'firstName' => 'Travis',
'lastName' => 'Dickerson',
'position' => 'Maintanance',
],
[
'id' => 17398683,
'firstName' => 'Maria',
'lastName' => 'Garcia',
'position' => 'Teacher',
],
[
'id' => 17398686,
'firstName' => 'Jacob',
'lastName' => 'Smith',
'position' => 'Teacher',
],
[
'id' => 13398706,
'firstName' => 'Jose',
'lastName' => 'Bautista',
'position' => 'BusDriver',
],
[
'id' => 14398838,
'firstName' => 'Michael',
'lastName' => 'Watson',
'position' => 'Principal',
],
[
'id' => 22398843,
'firstName' => 'Alex',
'lastName' => 'Hood',
'position' => 'Security',
],
];
If array has smaller set of member still output the same order:
$staff2 = [
[
'id' => 1599874,
'firstName' => 'Jim',
'lastName' => 'Turner',
'position' => 'Principal',
],
[
'id' => 12398646,
'firstName' => 'Travis',
'lastName' => 'Dickerson',
'position' => 'Maintenance',
],
[
'id' => 17398683,
'firstName' => 'Maria',
'lastName' => 'Garcia',
'position' => 'Teacher',
],
[
'id' => 13398706,
'firstName' => 'Jose',
'lastName' => 'Bautista',
'position' => 'BusDriver',
],
[
'id' => 22398843,
'firstName' => 'Alex',
'lastName' => 'Hood',
'position' => 'Security',
],
];