uasort() with sum

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
gedit
Forum Newbie
Posts: 2
Joined: Sun Jun 06, 2010 1:11 pm

uasort() with sum

Post by gedit »

Hello. I have a multi-dimensional array that looks something like this:

Code: Select all

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [name] => Black Cumin Seed Oil Nigella Sativa
                    [hardness] => 0.8
                    [cleansing] => 0
                    [condition] => 4.15
                )

            [1] => Array
                (
                    [name] => Coconut Oil 76 / 92 Deg
                    [hardness] => 19.75
                    [cleansing] => 16.75
                    [condition] => 2.5
                )

            [2] => Array
                (
                    [name] => Olive Oil
                    [hardness] => 11.9
                    [cleansing] => 0
                    [condition] => 57.4
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [name] => Black Cumin Seed Oil Nigella Sativa
                    [hardness] => 0
                    [cleansing] => 0
                    [condition] => 0
                )

            [1] => Array
                (
                    [name] => Coconut Oil 76 / 92 Deg
                    [hardness] => 23.7
                    [cleansing] => 20.1
                    [condition] => 3
                )

            [2] => Array
                (
                    [name] => Olive Oil
                    [hardness] => 11.9
                    [cleansing] => 0
                    [condition] => 57.4
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [name] => Black Cumin Seed Oil Nigella Sativa
                    [hardness] => 0
                    [cleansing] => 0
                    [condition] => 0
                )

            [1] => Array
                (
                    [name] => Coconut Oil 76 / 92 Deg
                    [hardness] => 19.75
                    [cleansing] => 16.75
                    [condition] => 2.5
                )

            [2] => Array
                (
                    [name] => Olive Oil
                    [hardness] => 12.75
                    [cleansing] => 0
                    [condition] => 61.5
                )

        )

)
What I'm trying to do is sort this by the "sum" of the "hardness" of all 3 oils. Is there a way to modify the typical compare($x, $y) function used with uasort() to account for sums?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: uasort() with sum

Post by Jonah Bron »

You're on the right track. Here's a function that you would pass as the callback function.

Code: Select all

function cmp($x, $y) {
    $sum1 = $x[0]['hardness'] + $x[1]['hardness'] + $x[2]['hardness'];
    $sum2 = $y[0]['hardness'] + $y[1]['hardness'] + $y[2]['hardness'];
    return ($sum1 > $sum2) ? -1 : 1;
}
Note: you should use usort(), not uasort().
Last edited by Jonah Bron on Sun Jun 06, 2010 4:02 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: uasort() with sum

Post by John Cartwright »

Not exactly the most elegant solution, but it should work (untested).

Code: Select all

function mySort($a, $b) {
    
    $asum = 0;
    foreach ($a as $row) $asum += $row['hardness'];

    $bsum = 0;
    foreach ($b as $row) $bsum += $row['hardness'];

    if ($asum == $bsum) {
        return 0;
    }
    return ($asum < $bsum) ? 1 : -1;    
} 
gedit
Forum Newbie
Posts: 2
Joined: Sun Jun 06, 2010 1:11 pm

Re: uasort() with sum

Post by gedit »

Thank you so much! :D
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: uasort() with sum

Post by Jonah Bron »

John Cartwright wrote:Not exactly the most elegant solution...
Mine or yours? :D
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: uasort() with sum

Post by John Cartwright »

Jonah Bron wrote:
John Cartwright wrote:Not exactly the most elegant solution...
Mine or yours? :D
I hadn't noticed your post actually. So, I guess both of ours. :D
Post Reply