Forum Moderators: coopster

Message Too Old, No Replies

php sorting

rsort / question sorting 2 arrays

         

ikbenhet1

2:49 pm on Feb 25, 2003 (gmt 0)

10+ Year Member



I have 2 arrays.

$votes("32","137","55","75");
$id("1","2","3","4");

I want to sort the array: $votes reverse descending.
so i use: rsort ($votes);

the result is: $votes("137","75","55","32");

now i want the array: $id to be sorted exactly like $votes has been sorted so the output of $id would be:

$id("2","4","3","1");

How/with what command can this be done?

bcolflesh

2:58 pm on Feb 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dear Ikbenhet1,
Wouldn't you JOIN the tables first in your query? Then you could tie the votes and ID together by feeding your query one of the arrays.

Regards,
Brent

jatar_k

3:00 pm on Feb 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Try using a multi dimensional array like so

$votes[0](32,137,55,75);
$votes[1](1,2,3,4);

and then use array_multisort [php.net]
array_multisort($votes[0],SORT_DESC,SORT_NUMERIC,$votes[1]);

hakre

3:05 pm on Feb 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi ikbenhet1,

take your two arrays:
$votes("32","137","55","75");
$id("1","2","3","4");

and create one of it:
$votes[1]=32;
$votes[2]=137;
$votes[3]=55;
$votes[4]=75;

if you use rsort then, the key of the array element will be your id. you can re-sort to id later on, too.

it's called an associative array an can help a lot. i'm glad php has this build in.

andreasfriedrich

3:06 pm on Feb 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>it's called an associative array an can help a lot

Itīs rather unfortunate that they named these thingies associative arrays though. Hash is a lot shorter ;).

Another way would be to use an associative array and a[url=http://www.php.net/rsort]r[url=http://www.php.net/sort]sort() [php.net][/url][/url] it. Which is better/faster/nicer/more convenient will depend on how you build your arrays in the first place.

Andreas

<added>hakre beat me to it, creating that link took some time ;)</added>

[edited by: andreasfriedrich at 3:32 pm (utc) on Feb. 25, 2003]

hakre

3:21 pm on Feb 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<added>hakre beat me to it, creating that link took some time ;) </added>

yo andreasfriedrich,

but the hash naming is quite alright as well i think. ;) just was searching for the right word and associative was comming into my mind. i think even in perl you can name it both ways.

for a detailed view on arrays in php, checkout the docs [php.net]. for the discussion it's good to know that there are no differences between an scalar indexed or associative array in php. it got only one type of array.

Birdman

3:25 pm on Feb 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>>creating that link took some time

I thought that was yet another addition to your auto-link system. :) I gotta get one of those!

andreasfriedrich

3:31 pm on Feb 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>>>>>creating that link took some time

>>>I thought that was yet another addition to your auto-link
>>>system. :)

It is and I was only kidding. But the extra proxy server does add some micro seconds to the travel time of those ip packets.

Andreas

ikbenhet1

3:39 pm on Feb 25, 2003 (gmt 0)

10+ Year Member




wow, that's a lotto replies.
i will trie all suggestions 1 by 1.

Ok, first i've tried what jatar said:

$votes[0](32,137,55,75);
$votes[1](1,2,3,4);
array_multisort($votes[0],SORT_DESC,SORT_NUMERIC,$votes[1]);
echo $votes[0];
echo $votes[1];
echo $votes[2];
echo $votes[3];
echo $id[0];
echo $id[1];
echo $id[2];
echo $id[3];

i get : Call to undefined function: () on line 3

i'm upp to try the next suggestion in this thread.

ikbenhet1

3:52 pm on Feb 25, 2003 (gmt 0)

10+ Year Member




Hakre, i think then i will lose my voteid. let me try to explain(:

$id[0]=1; $votes[0]=100;
This means the owner of profile nr 1 has 100 votes.

$id[1]=2; $votes[1]=150;
this means the owner of profile nr-2 has 150 votes.

$id[2]=3; $votes[2]=200;
this means the owner of profile nr-3 has 200 votes.

$id[3]=100; $votes[3]=160;
this means the owner of profile nr-100 has 160 votes.

i want to sort so:

arraynr $Votes $Id
[0] - 200 - 3
[1] - 160 - 100
[2] - 150 - 2
[3] - 100 - 1

the $votes sorting is easy, i need to sort $id:

hakre

3:58 pm on Feb 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hakre, i think then i will lose my voteid. let me try to explain(:

no that's not true. if you take my proposal, this will be the resulting array:

$votes[2]=137;
$votes[4]=75;
$votes[3]=55;
$votes[1]=32;

you can use print_r() to verify what i'm saying. the array will be ordered and the key=>value pairs will be untouched. now you can say: first is id 2 with 137 votes.

ikbenhet1

4:17 pm on Feb 25, 2003 (gmt 0)

10+ Year Member



Hakre,

$votes=32;
$votes[2]=137;
$votes[3]=55;
$votes[4]=75;

rsort($votes);
print_r($votes);

gives:

Array ( [0] => 137 [1] => 75 [2] => 55 [3] => 32 )

so 137 votes for who?
so 75 votes for who?

<added>oh sory, the array now indeed has the values you said it would. i'll try to figure it out. Thanks</added>

[1][edited by: ikbenhet1 at 4:23 pm (utc) on Feb. 25, 2003]

andreasfriedrich

4:22 pm on Feb 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try using a[url=http://www.php.net/rsort]r[url=http://www.php.net/sort]sort() [php.net][/url][/url] as posted above.

andreasfriedrich

4:25 pm on Feb 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$votes[1]=32;  
$votes[2]=137;
$votes[3]=55;
$votes[4]=75;
#
arsort($votes);
print_r($votes);

will print_r() [php.net]

Array ( [2] => 137 [4] => 75 [3] => 55 [1] => 32 ) 

Andreas

hakre

4:34 pm on Feb 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



sorry myfault, i assumed arsort() is used. ;) this solves the prob.

ikbenhet1

4:58 pm on Feb 25, 2003 (gmt 0)

10+ Year Member



Final question:

The print_r command outputs:

Array ( [2] => 137 [4] => 75 [3] => 55 [1] => 32 )

How can i extract the arraynumbers in this sorted order?

Like this:

$winner[0]=2
$winner[1]=4
$winner[2]=3
$winner[3]=1

brotherhood of LAN

5:18 pm on Feb 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



>how would you get teh array numbers

foreach($arraynumber as $key => $num)
{
echo '$winner[',$key,']',$num,'<br>';
}

if you want to sort them by key, look into ksort() and krsort()

andreasfriedrich

5:29 pm on Feb 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>>if you want to sort them by key...

After all the trouble we have been through to get them unordered you actually dare to suggest that we might want to order the hash by key ;). Thatīs a no no here bol ;)

Andreas

ikbenhet1

5:37 pm on Feb 25, 2003 (gmt 0)

10+ Year Member




I't works! I would never have figured this one out by myself.
Thanks to all you guy's.