manipulating arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

manipulating arrays

Post 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 :)
Last edited by Dave2000 on Tue Jan 09, 2007 7:12 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
Post Reply