Page 1 of 1

uasort() with sum

Posted: Sun Jun 06, 2010 1:32 pm
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?

Re: uasort() with sum

Posted: Sun Jun 06, 2010 2:17 pm
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().

Re: uasort() with sum

Posted: Sun Jun 06, 2010 2:27 pm
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;    
} 

Re: uasort() with sum

Posted: Sun Jun 06, 2010 2:59 pm
by gedit
Thank you so much! :D

Re: uasort() with sum

Posted: Sun Jun 06, 2010 4:07 pm
by Jonah Bron
John Cartwright wrote:Not exactly the most elegant solution...
Mine or yours? :D

Re: uasort() with sum

Posted: Sun Jun 06, 2010 4:56 pm
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