Page 1 of 1

manipulating arrays

Posted: Tue Jan 09, 2007 4:26 pm
by Dave2000
I am attempting to rank some values in a game I am making.

Code: Select all

$attack_values = array(0 => 12, 1 => 18, 2 => 15, 3 =>16); // ie. userid 0 has an attack value of 12, userid 1 has an attack value of 18 etc...

arsort($attack_values); // rank the users

$attack_values= array_flip($attack_values);
So, array $attack_values is now as follows: The first element has the userid (for its value) of the user with the highest attack, the second element has the userid (for its value) of the element with the second highest attack.

I want to make a new array called $attack_ranks() mapping the userid to the element number from $attack_values that the userid occurred at.

Code: Select all

$attack_ranks = array();
$i = 0;
while ($i < 5) {
$attack_ranks[] = $attack_values[$i];
$i++;
}

foreach($attack_ranks as $key => $value)
{
  echo "$key = $value<br/>";
}
My problem is in the while loop. I cant get the value of $attacks[$i] (which is the userid) into the array $attack_ranks(). I get an output as shown below...
0 =
1 =
2 =
3 =
4 =
Thank you for any help.

Shears :)

Posted: Tue Jan 09, 2007 7:05 pm
by feyd
You don't have an $attacks array shown.

Is it not possible that users can have the same attack value?

array_values() could possibly be of interest.

Posted: Tue Jan 09, 2007 8:10 pm
by Ollie Saunders
Shears, could you take a step back and tell me what data you are storing, why, and how you intend to use it. From that I'll probably be able to suggest the easiest way to store your data.