I am trying to order the elements of an array by the following two ways:
- Sort by key length (smallest should go first) - See here
- Max value should go first
To sort by key length I use this:
function sortByLength($a,$b){
if($a == $b) return 0;
return (strlen($a) < strlen($b) ? -1 : 1);
}
uasort($deals_tag,'sortByLength');
And the array is sorted that way:
Array
(
[/merchantProductFeed] => 1
[/merchantProductFeed/merchant] => 1
[/merchantProductFeed/merchant/prod/text] => 158
[/merchantProductFeed/merchant/prod] => 158
)
I want it to be sorted like this:
Array
(
[/merchantProductFeed/merchant/prod] => 158
[/merchantProductFeed/merchant/prod/text] => 158
[/merchantProductFeed] => 1
[/merchantProductFeed/merchant] => 1
)
Notice that first the array is sorted by key length and then by max value.
I don't know how to do that.
Any help would be highly appreciated! Thank you!
sortByLength()
:return strlen($a) - strlen($b);
uasort
is sort by value, not by key.