Array Sorting Madness
Posted: Mon Aug 04, 2008 6:35 am
Here is the situation. I have a multidimensional array that I am trying to sort by 2 values. Let me example your face:
So I have that array. What I want to do is first group it by the 'paymentMethod'. So after the first sort it would look like this:
So all the payment methods are together. Next I want to order those payment methods by the totalPayment from higest to lowest but still retaining the paymentMethod grouping. So in the end, I want the original array to look like this:
and that would be that. The only way I can think of doing this now is to break up the whole array into smaller arrays, 1 for each paymentMethod, then using a nice little usort() call on each array then building them all back together but this just seams wasteful to me. So, I ask you guys, do you know a simpler way to do this? Some angle I haven't thought about maybe?
Code: Select all
Array
(
[0] => Array
(
[username] => guy1
[paymentMethod] => Paypal
[totalPayment] => 10.33
)
[1] => Array
(
[username] => guy2
[paymentMethod] => McDonalds
[totalPayment] => 8.41
)
[2] => Array
(
[username] => guy3
[paymentMethod] => Paypal
[totalPayment] => 1.00
)
[3] => Array
(
[username] => guy4
[paymentMethod] => Pizza Hut
[totalPayment] => 75.1
)
[4] => Array
(
[username] => guy5
[paymentMethod] => McDonalds
[totalPayment] => 50.22
)
)
Code: Select all
Array
(
[0] => Array
(
[username] => guy2
[paymentMethod] => McDonalds
[totalPayment] => 8.41
)
[1] => Array
(
[username] => guy5
[paymentMethod] => McDonalds
[totalPayment] => 50.22
)
[2] => Array
(
[username] => guy1
[paymentMethod] => Paypal
[totalPayment] => 10.33
)
[3] => Array
(
[username] => guy3
[paymentMethod] => Paypal
[totalPayment] => 1.00
)
[4] => Array
(
[username] => guy4
[paymentMethod] => Pizza Hut
[totalPayment] => 75.1
)
)
Code: Select all
Array
(
[1] => Array
(
[username] => guy5
[paymentMethod] => McDonalds
[totalPayment] => 50.22
)
[0] => Array
(
[username] => guy2
[paymentMethod] => McDonalds
[totalPayment] => 8.41
)
[2] => Array
(
[username] => guy1
[paymentMethod] => Paypal
[totalPayment] => 10.33
)
[3] => Array
(
[username] => guy3
[paymentMethod] => Paypal
[totalPayment] => 1.00
)
[4] => Array
(
[username] => guy4
[paymentMethod] => Pizza Hut
[totalPayment] => 75.1
)
)