Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/147949-merging-2-fieldsarrays/
Share on other sites

$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.

$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.

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.

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
)

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'

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.