You are here: Re: Array « PHP Programming Language « IT news, forums, messages
Re: Array

Posted by Joe on 11/27/05 00:49

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:
>> 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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация