Mutley Posted March 4, 2009 Share Posted March 4, 2009 Not sure what a suitable topic title would be, I have 2 fields that are used to explode in to arrays, one is "Users" the other is "Points", they are both ordered so the position of the Points are of equal match to that user. For example: Users: 1;7;5;3;6;2 Points: 10;12;8;11;8;9 So User '1' has 10 points, User '7' has 12 points. What I wish to do is allocate a variable to the first 3 Users who have the most Points (1st, 2nd and 3rd) so I am able to display a message congratulating them on their winning place, then a message to everyone else after this, to let them know they've lost. I need to basically order the arrays in relation to each other, just how would I do this so they don't muddle up? Thanks in Advance, Nick. Quote Link to comment https://forums.phpfreaks.com/topic/147949-merging-2-fieldsarrays/ Share on other sites More sharing options...
premiso Posted March 4, 2009 Share Posted March 4, 2009 $users = '1;7;5;3;6;2'; $users = explode(";", $users); $points = '10;12;8;11;8;9'; $points = explode(";", $points); $cnt = (count($users)-1); for ($i=0;$i<$cnt;$i++) { $userPoints[$users[$i]] = $point[$i]; } print_r($userPoints); Give that a try. There might be a better way, but that should work. Quote Link to comment https://forums.phpfreaks.com/topic/147949-merging-2-fieldsarrays/#findComment-776526 Share on other sites More sharing options...
Mutley Posted March 4, 2009 Author Share Posted March 4, 2009 Thanks, premiso, so basically count the results from the explode then print them with the points by the side. How would you reorder them by Points descending? Quote Link to comment https://forums.phpfreaks.com/topic/147949-merging-2-fieldsarrays/#findComment-776530 Share on other sites More sharing options...
tivrfoa Posted March 4, 2009 Share Posted March 4, 2009 you can use associative array like: $userPoints = array(1 => 10, 7 => 12, 5 => 8, 3 => 11, 6 => 8, 2 => 9); Quote Link to comment https://forums.phpfreaks.com/topic/147949-merging-2-fieldsarrays/#findComment-776533 Share on other sites More sharing options...
premiso Posted March 4, 2009 Share Posted March 4, 2009 $users = '1;7;5;3;6;2'; $users = explode(";", $users); $points = '10;12;8;11;8;9'; $points = explode(";", $points); $cnt = (count($users)-1); for ($i=0;$i<$cnt;$i++) { $userPoints[$users[$i]] = $point[$i]; } rsort($userPoints); print_r($userPoints); sort look at the different sort commands. Quote Link to comment https://forums.phpfreaks.com/topic/147949-merging-2-fieldsarrays/#findComment-776550 Share on other sites More sharing options...
Mutley Posted March 6, 2009 Author Share Posted March 6, 2009 Cheers, just a few issues though, not sure if I understand this too well. Firstly I removed the -1 from the count, as it was a single result too short. It echos this with no "rsort": Array ( [1] => [7] => [5] => [3] => [6] => [2] => ) Okay, now where are the points? Now when I do use "rsort", I just numbers that are of no significance, like this: Array ( [0] => [1] => [2] => [3] => [4] => [5] => ) Finally, how do I call, say the first result of the array? I tried print_r($userPoints[1]); but got nothng, is this wrong? Many thanks, Nick. Quote Link to comment https://forums.phpfreaks.com/topic/147949-merging-2-fieldsarrays/#findComment-778422 Share on other sites More sharing options...
premiso Posted March 6, 2009 Share Posted March 6, 2009 $userPoints[$users[$i]] = $points[$i]; Modify that part as it should be $points not $point. That was my mistake. Quote Link to comment https://forums.phpfreaks.com/topic/147949-merging-2-fieldsarrays/#findComment-778424 Share on other sites More sharing options...
Philip Posted March 6, 2009 Share Posted March 6, 2009 I'd use array_combine() $users = '1;7;5;3;6;2'; $users = explode(";", $users); $points = '10;12;8;11;8;9'; $points = explode(";", $points); $array = array_combine($users, $points); // combine, use users as key, $points as value arsort($array); // sort, maintaining the correct keys, from highest to lowest points echo '<pre>'; print_r($array); // show what outputs echo '</pre>'; Outputs: Array ( [7] => 12 [3] => 11 [1] => 10 [2] => 9 [5] => 8 [6] => 8 ) Quote Link to comment https://forums.phpfreaks.com/topic/147949-merging-2-fieldsarrays/#findComment-778488 Share on other sites More sharing options...
premiso Posted March 6, 2009 Share Posted March 6, 2009 I'd use array_combine() Yea, I guess I overlooked that when looking through the array functions Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/147949-merging-2-fieldsarrays/#findComment-778492 Share on other sites More sharing options...
Mutley Posted March 14, 2009 Author Share Posted March 14, 2009 I see, now how could I attach a variable to the 1st array result? Say if I wanted to send a message saying "Congratulations, you've won!"? Would it be possible to do something like: Position => User ID => Score Array ( [1] => [7] => 12 [2] => [3] => 11 [3] => [1] => 10 [4] => [2] => 9 [5] => [5] => 8 [6] => [6] => 8 ) $winner=$array[6]; print_r($array[6]; // prints as.... '7' Quote Link to comment https://forums.phpfreaks.com/topic/147949-merging-2-fieldsarrays/#findComment-784647 Share on other sites More sharing options...
Mutley Posted March 14, 2009 Author Share Posted March 14, 2009 I've been doing a little reasearch and there are multidimensional arrays, I could also do a foreach to assing a variable to each position? Anyone know more about multidimensional arrays or am I being over complicated? Thanks in advance, Mutley. Quote Link to comment https://forums.phpfreaks.com/topic/147949-merging-2-fieldsarrays/#findComment-784687 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.