This question already has an answer here:
- Sort array of objects by object fields 18 answers
How do is sort this object by 'pos' in php?
Array (
[0] => stdClass Object ( [str] => Mondays [pos] => 170 )
[1] => stdClass Object ( [str] => Tuesdays [pos] => 299 )
[2] => stdClass Object ( [str] => Wednesdays [pos] => 355 )
[3] => stdClass Object ( [str] => Thursdays [pos] => 469 )
[4] => stdClass Object ( [str] => Fridays [pos] => 645 )
[5] => stdClass Object ( [str] => Mondays [pos] => 972 )
[6] => stdClass Object ( [str] => Tuesdays [pos] => 1033 )
[7] => stdClass Object ( [str] => Thursdays [pos] => 1080 )
[8] => stdClass Object ( [str] => Fridays [pos] => 1180 )
)
You could probably use the usort() family of functions to sort it either on
str
orpos
. You have to define your own comparison function for that.Pseudo-PHP example:
Depending on your needs, other comparison functions might be more appropriate.
Try this function