For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > November 2005 > Array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Array
news.inet.tele.dk

2005-11-25, 6:57 pm

Hi
Can you help me sort my array on salary

$Person[$personid][salary]

e.g.
$Person[1][20000]
$Person[2][5000]
$Person[3][15000]
$Person[4][10000]

to
$Person[2][5000]
$Person[4][10000]
$Person[3][15000]
$Person[1][20000]


Thanx



Ewoud Dronkert

2005-11-25, 6:57 pm

news.inet.tele.dk wrote:
> Can you help me sort my array on salary


http://php.net/usort 2nd example.

--
E. Dronkert
Joe

2005-11-26, 3:55 am

Care to show how?

"Ewoud Dronkert" <firstname@lastname.net.invalid> skrev i en meddelelse
news:i8seo19pke16ih80l1b5jv87uopsjsi4ge@
4ax.com...
> news.inet.tele.dk wrote:
>
> http://php.net/usort 2nd example.
>
> --
> E. Dronkert



Ewoud Dronkert

2005-11-26, 7:55 am

Joe wrote:[color=darkred]
> Care to show how?
>

Yes topposter: exactly according to example no. 2 on http://php.net/usort

--
E. Dronkert
Joe

2005-11-26, 6:56 pm

As I see it it can sort the values but doesn't keep the personid fixed to
the salary.





"Ewoud Dronkert" <firstname@lastname.net.invalid> skrev i en meddelelse
news:qsfgo1p0isn0n6l9eigc38l4nl52scgm19@
4ax.com...
> Joe wrote:
>
> Yes topposter: exactly according to example no. 2 on http://php.net/usort
>
> --
> E. Dronkert



Ewoud Dronkert

2005-11-26, 6:56 pm

Joe wrote:
> As I see it it can sort the values but doesn't keep the personid fixed to
> the salary.


Is that important? I don't know. I mean, where's the real data? He only
showed us the keys of a 2D array. Very weird to use salary as the 2nd key!
And, is there only one index value for each personid??

So, use uasort instead of usort:

function cmp($a, $b) {
list($k1,) = each($a);
list($k2,) = each($b);
return $k1 - $k2;
}
uasort($person, 'cmp');

Or:

$k = array_keys($person);
foreach ( $k as $i )
list($salary[$i],) = each($person[$i]);
asort($salary);

--
E. Dronkert
Ewoud Dronkert

2005-11-26, 6:56 pm

Ewoud Dronkert wrote:
> And, is there only one index value for each personid??


One salary figure, I mean. It do seems likely to have only one salary per
person, also the sorting wouldn't make sense otherwise.

A far better data structure would probably be, in pseudo code:
$person[] = array('id' => (int), 'name' => (string), 'salary' => (float));

Then the solution would be:

function cmp($a, $b) {
return $a['salary'] - $b['salary'];
}

Or a little safer:

function cmp($a, $b) {
$e = 0.01;
$t = $a['salary'] - $b['salary'];
if ( $t >= $e ) return 1;
if ( $t <= -$e ) return -1;
return 0;
}

Followed by:

usort($person, 'cmp');
foreach ( $person as $p )
printf('%3d %-20s %8.2f', $p['id'], $p['name'], $p['salary']);

--
E. Dronkert
Joe

2005-11-26, 6:56 pm

That worked perfect, thank you very much.

Next problem is for me to calculate:
Total and avg. value of the 25 highest paid workers
Total and avg. value of the 50 highest paid workers
I can do it by looping through the array, is there a way to do it in one
command?



"Ewoud Dronkert" <firstname@lastname.net.invalid> skrev i en meddelelse
news:8p2ho15hauhj7hhdblkltrll9tdo9rk0ln@
4ax.com...
> Ewoud Dronkert wrote:
>
> One salary figure, I mean. It do seems likely to have only one salary per
> person, also the sorting wouldn't make sense otherwise.
>
> A far better data structure would probably be, in pseudo code:
> $person[] = array('id' => (int), 'name' => (string), 'salary' => (float));
>
> Then the solution would be:
>
> function cmp($a, $b) {
> return $a['salary'] - $b['salary'];
> }
>
> Or a little safer:
>
> function cmp($a, $b) {
> $e = 0.01;
> $t = $a['salary'] - $b['salary'];
> if ( $t >= $e ) return 1;
> if ( $t <= -$e ) return -1;
> return 0;
> }
>
> Followed by:
>
> usort($person, 'cmp');
> foreach ( $person as $p )
> printf('%3d %-20s %8.2f', $p['id'], $p['name'], $p['salary']);
>
> --
> E. Dronkert



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com