Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Array

 
   Database Forums (Home) -> PHP RSS
Next:  Problem with mysqli stmt and multi queris.  
Author Message
news.inet.tele.dk

External


Since: Nov 25, 2005
Posts: 1



(Msg. 1) Posted: Fri Nov 25, 2005 3:55 pm
Post subject: Array
Archived from groups: comp>lang>php (more info?)

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

 >> Stay informed about: Array 
Back to top
Login to vote
firstname

External


Since: Jan 23, 2005
Posts: 22



(Msg. 2) Posted: Sat Nov 26, 2005 11:55 am
Post subject: Re: Array [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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

 >> Stay informed about: Array 
Back to top
Login to vote
firstname

External


Since: Jan 23, 2005
Posts: 22



(Msg. 3) Posted: Sat Nov 26, 2005 12:55 pm
Post subject: Re: Array [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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
 >> Stay informed about: Array 
Back to top
Login to vote
Joe

External


Since: Nov 26, 2005
Posts: 6



(Msg. 4) Posted: Sat Nov 26, 2005 6:55 pm
Post subject: Re: Array [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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" skrev i en meddelelse

> 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
 >> Stay informed about: Array 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
accessing array data inside of an array - I want to extract the name of the photo uploaded. print_r of $_FILES yields: Files: Array ( [picture] => Array ( [name] => DSC_1802-saoirse-riona.jpg [type] => image/jpeg [tmp_name] => /tmp/phpJTspHZ [error] => 0 [size] => 76096 ) ) ...

SUM of array - I want to get the sum of the array key values. ex: $key1=>10, $key2=>15, $key3=>5 I want variable $sum = 30. any solution?

array() VS Array() - which one is recommend? seems they perform the same thing... regards, howa

Last element of array ? - I need the first, last and middle elements of a (single dimension) array during a loop which may then add 0..n elements to it so I don't know how many elements are in it the next run. Right now I use: [....] $len = count($arr); $var0 = $arr[0];...

assosiativ array - I have this array, $navbar_left = array( 'Hjem'=>'index.php', 'Borte'=>'borte.php', 'Uavgjort'=>'uavgjort.php' ); I want to loop through it, but don't know how. I've tried foreach($navbar_left as $navbar){ foreach($navbar as $key=>$value){ ...
   Database Forums (Home) -> PHP All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]