I have some simple PHP code that formats an object containing information about some songs. I try and sort this information using usort(), but I can't quite get the sorting to work correctly.
print_r($my_songs_object)
returns (heavily simplified):
stdClass Object
(
[0] => stdClass Object
(
[track] => stdClass Object
(
[name] => ZZZ
)
)
[1] => stdClass Object
(
[track] => stdClass Object
(
[name] => YYY
)
)
)
My PHP sorting code:
usort($my_songs_object, function($a, $b) {
return ucfirst($a->track->name) > ucfirst($b->track->name) ? 1 : -1;
});
After running the above usort()
, $my_songs_object
always returns the data sequentially.
In the above example, item 1 should come before item 0.
How can I get modify the usort function to achieve the desired output?
EDIT:
The original object is being created an array_merge of two preexisting objects that need to be combined. I am using the following code to combine them:
$my_songs_object = (object)array_merge((array)$a->items, (array)$b->items);
Perhaps modifying the way in which the objects are combined would make sorting the combined $my_songs_object easier?