[PHP] Sort multidimensional array - ARGH!

2004-02-05 Thread Kim Steinhaug
I have been messing around now for some hours and Im going mad!
Im pulling some heavy data from mySQL with count and grouping,
after this I have to calculate som values and then sort the outcome.
Therefore Im not able to let mySQL do the accuall sort.

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

1. I declare an array :

$stats_unsorted = array();

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

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

We now have, e.g. this :
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
)
)

- - - - - - - - - - - - - - - - - - - -

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

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

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

Help would be appretiated!

$stats_SORTED = ???;

Thanks in advance!

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

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



Re: [PHP] Sort multidimensional array - ARGH!

2004-02-05 Thread Tom Rogers
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