Array Sorting Madness

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Array Sorting Madness

Post by shiznatix »

Here is the situation. I have a multidimensional array that I am trying to sort by 2 values. Let me example your face:

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
        )
 
)
 
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:

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
        )
 
)
 
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:

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
        )
 
)
 
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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Array Sorting Madness

Post by VladSun »

Untested:

Code: Select all

function cmp($a, $b)
{
    if ($a['paymentMethod'] == $b['paymentMethod']) {
        if ($a['totalPayment'] == $b['totalPayment']) {
              return 0;
        }
        return ($a['totalPayment'] < $b['totalPayment']) ? -1 : 1;
    }
    return ($a['paymentMethod'] < $b['paymentMethod']) ? -1 : 1;
}
 
usort($array, "cmp");
 
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Array Sorting Madness

Post by shiznatix »

Ha super thanks. That works perfectly. I think i need to get more sleep, I can't function anymore.
Post Reply