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

Posted by Ewoud Dronkert on 11/26/05 18:41

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

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