I have combined two arrays using array_merge, and now need to sort them alphabetically. Problem is I need to compare two different keys,
e.g.
[0] => stdClass Object
(
[id] => 16
[title] => Oranges
)
[1] => stdClass Object
(
[id] => 23
[title] => Apples
)
[2] => stdClass Object
(
[id] => 16
[name] => Bananas
)
I need to compare 'name' properties with each other, as well as with 'title' properties, then sort those alphabetically, (ignoring the id) so that my result would be:
Apples
Bananas
Oranges
However, the function I have here:
function compareItems($a, $b)
{
if ( $a->title < $b->title ) return -1;
if ( $a->title > $b->title ) return 1;
if ( $a->name < $b->name ) return -1;
if ( $a->name > $b->name ) return 1;
return 0;
}
usort($array, "compareItems");
...doesn't combine, but sorts one set of properties (title) then the other (name), thus giving me:
Apples
Oranges
Bananas
I'm hoping I can amend this function to merge the two together - I'm not really a PHP programmer, so any help is much appreciated!
Thanks in advance.