Im so tired of finding the way to sort this multidimensional array.
I have tried usort()
but it doesnt sort the way I want to. Or just maybe I could not figure out the correct logic to use. usort()
seems hard to understand for me.
I wanted to sort my data(example below), by finding first which has the higher value (between keys a and b) for each of the sub arrays. And then the sub array which has the highest value either from its keys a or b will be on top.
For example this array:
Array
(
[0] => Array
(
[a]=>5
[b]=>
[c]=>apple
)
[1] => Array
(
[a]=>5
[b]=>7
[c]=>guava
)
[2] => Array
(
[a]=>6
[b]=>
[c]=>banana
)
[3] => Array
(
[a]=>5
[b]=>
[c]=>avocado
)
)
should be sorted like this:
Array
(
[0] => Array
(
[a]=>5
[b]=>7
[c]=>guava
)
[1] => Array
(
[a]=>6
[b]=>
[c]=>banana
)
[2] => Array
(
[a]=>5
[b]=>
[c]=>apple
)
[3] => Array
(
[a]=>5
[b]=>
[c]=>avocado
)
So how exactly I am able to do this? Im so confused how to use usort. What is the best PHP function for sorting this?