Forum Moderators: coopster
$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?
$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]);
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.
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]
<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.
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.
$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, 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.
$votes=32; rsort($votes); gives: Array ( [0] => 137 [1] => 75 [2] => 55 [3] => 32 ) so 137 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]
$votes[2]=137;
$votes[3]=55;
$votes[4]=75;
print_r($votes);
so 75 votes for who?
$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