Hi,

Thursday, February 5, 2004, 11:35:34 PM, you wrote:
KS> I have been messing around now for some hours and Im going mad!
KS> Im pulling some heavy data from mySQL with count and grouping,
KS> after this I have to calculate som values and then sort the outcome.
KS> Therefore Im not able to let mySQL do the accuall sort.

KS> So i stuff it into an array, and I need to sort the array and thats where
KS> the problem occures.

KS> 1. I declare an array :

KS> $stats_unsorted = array();

KS>     for loop start ...
KS>     1. fetch mySQL data
KS>     2. calculate values

KS>     array_push($stats_unsorted,$sum,$user);
KS>         // Eks. array_push($stats_unsorted,10,Ola);
KS>         // Eks. array_push($stats_unsorted,-5,Kaare);
KS>         // Eks. array_push($stats_unsorted,203,Nils);
KS>         // Eks. array_push($stats_unsorted,-20,Lars);
KS>     for loop end ...

KS> We now have, e.g. this :
KS> Array
KS> (
KS>     [0] => Array
KS>         (
KS>             [0] => 10
KS>             [1] => Ola
KS>         )

KS>     [1] => Array
KS>         (
KS>             [0] => -5
KS>             [1] => Kaare
KS>         )

KS>     [2] => Array
KS>         (
KS>             [0] => 203
KS>             [1] => Nils
KS>         )

KS>     [3] => Array
KS>         (
KS>             [0] => -20
KS>             [1] => Tone
KS>         )
KS> )

KS> - - - - - - - - - - - - - - - - - - - -

KS> Then comes the sorting part, I want to sort by this :
KS> $stats_unsorted[x][0]

KS> meaning the numbers, ofcourse I would surely like to know
KS> how to sort by the names aswell, $stats_unsorted[x][1],
KS> but I would think that is easy if I could figure out the
KS> main command to sort this in the first place.

KS> Ive tried several times with array_multisort(); but I cant get this
KS> to work, :(

KS> Help would be appretiated!

KS> $stats_SORTED = ???;

KS> Thanks in advance!

KS> -- 
KS> Kim Steinhaug
KS> ---------------------------------------------------------------
KS> There are 10 types of people when it comes to binary numbers:
KS> those who understand them, and those who don't.
KS> ---------------------------------------------------------------


You can use usort for multi dimension arrays:


<?php
function cmp($a, $b) {
        if ($a[0] == $b[0]) {
                        return 0;
        }
        return ($a[0] < $b[0]) ? -1 : 1;
}

$a = array(
                        0 => Array(0 => 10,1 => 'Ola'),
                        1 => Array(0 => -5,1 => 'Kaare'),
                        2 => Array(0 => 203,1 => 'Nils'),
                        3 => Array(0 => -20,1 => 'Tone'));

usort($a, "cmp");

print_r($a);

?>

-- 
regards,
Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to