Nick Savage
Nick Savage

Reputation: 876

How to loop through multidimensional arrays and perform queries accordingly?

Well it's late so I assume I'm just exhausted but I've been trying to work through this for about an hour now and I can't find a solution that works for me. Basically the code queries the database to find out how many people are in a game, it then does a little bit of math to compare points between people. Afterwards it populates an array with an id unique to the player and their points. Now the problem is is that I'm trying to compare the array that has the list of people and their points with an array that holds the points the players will be awarded depending on their rank. I can't seem to find out how to pull the two arrays together and distribute points accordingly. Here's the code:

$game_id = 4; //will be replaced later on, just for testing
$x = 0;
$y = 0;
$point_array = array();

$entrantsquery = mysql_query("SELECT * FROM game_entrants WHERE game_id='$game_id' AND discipline=1");

while($entrantsarray = mysql_fetch_array($entrantsquery , MYSQL_ASSOC)){
    $statquery = mysql_query("SELECT * FROM player_stats WHERE player_id={$entrantsarray['player_id']}");
    $statarray = mysql_fetch_array($statquery, MYSQL_ASSOC);
    $points = $statarray['stat1'] + $statarray['stat2'] + ($statarray['stat3']*2.3) + $statarray['stat4'] + ($statarray['stat5']*2.7);
    $point_array[$x][$y] = $entrantsarray['player_id'];
    $y++;
    $point_array[$x][$y] = $points;
    $x++;
    $y--;
    }

Any help would be appreciated as this has started to give me a headache.

Edit:

Here's the code including the other array as well:

$total_points = 5000;
$distribution_quantity = 25;
$M = $distribution_quantity-1;
$K = ($distribution_quantity * $M) / 2;
$difference = 15;
$award_points = array();

$equation = ($total_points - ($K * $difference))/$distribution_quantity;

for ($z = $distribution_quantity; $z > 0; $z--) {
$award_points[] = $equation = $equation+$difference;
}

foreach ($award_points as $value) {
    echo $value. "<br>";

}

Newest Update:

I think you're misunderstanding me.

I have two arrays. The first array is comprised of a list of user's unique ids and points from a match. The player that has the most points from the match is #1. Now my second array carries data that is comprised of a list of 25 numbers. Each number represents a ranking in the match. 1 is for the best player and 25 is for the worst. each rank has a DIFFERENT set of points that are going to be added to a player's running total. The points from the first array are only used to determine where the player ranks overall for the match.

For example.

Player 1 gets 240 points in one match. Player 2 gets 180 points. Player 3 gets 210. And so on and so forth.

This means Player 1 took 1st place. Player 3 took 2nd place. Player 2 took 3rd place.

Now let's say that the second array looks like this:

1 -> 500 2 -> 300 3 -> 100

Player 1 gets 500 points. Player 2 gets 100 points. Player 3 gets 300 points.

I hope this makes it a bit more clear.

Upvotes: 0

Views: 153

Answers (3)

Abhay
Abhay

Reputation: 6645

I think changing the structure of $award_points might help, such that the score is the key and rank is the value.

array {
   345 => 1,
   267 => 2,
   265 => 3,
   .
   .   
}

Now, you may simply loop through $point_array and fetch corresponding rank from $award_points.

foreach ($point_array as $key => $arr) {
    $point_array[$key]['rank'] = $award_points[$arr['points']]
}
print_r($point_array);

I haven't tested the code but I think it should work.

EDIT BASED ON THE NEWEST UPDATE TO THE QUESTION

First, let us modify your code for populating $point_array like below:

$game_id = 4; //will be replaced later on, just for testing
$point_array = array();

$entrantsquery = mysql_query("SELECT * FROM game_entrants WHERE game_id='$game_id' AND discipline=1");
while($entrantsarray = mysql_fetch_array($entrantsquery , MYSQL_ASSOC)){
    $statquery = mysql_query("SELECT * FROM player_stats WHERE player_id={$entrantsarray['player_id']}");
    $statarray = mysql_fetch_array($statquery, MYSQL_ASSOC);
    $points = $statarray['stat1'] + $statarray['stat2'] + ($statarray['stat3']*2.3) + $statarray['stat4'] + ($statarray['stat5']*2.7);
    $point_array[$entrantsarray['player_id']] = $points;
}
arsort($point_array);

Note the arsort() method in the end. This will give the following structure to $point_array (based on the example from your latest update) and arrange in descending order of their points:

$point_array = Array (
        1 => 240,
        3 => 210,
        2 => 180
    )

Now, assuming that $award_points has the following structure:

$award_points = Array (
        1 => 500,
        2 => 300,
        3 => 100
    )

The below code should now give you the desired resultset, which is adding the award points to the players earned score according to what rank did the achieve

$prev_player_point = 0;
$read_rank = 0;
foreach ($point_array as $player_id => $player_point) {
    if ($player_point != $prev_player_point) {
        ++$read_rank;
    }
    $new_point_array[$player_id] = $player_point + $award_points[$read_rank];
    $prev_player_point = $player_point;
}
print_r($new_point_array);

I hope I understood your need and the above makes sense.

Upvotes: 1

Baba
Baba

Reputation: 95101

Would have helped but there are so many thing

  1. Using $statquery twice for mysql_query & mysql_fetch_array is a bad idea

  2. The following code does not make sense to me (am not sure to others )

    $point_array[$x][$y] = $entrantsarray['player_id'];
    $y++;
    $point_array[$x][$y] = $points;
    
  3. Where does your $award_points come to pay in your first code ...

With more information .. i can update this answer with a much more improved code

========================= UPDATE =======================

Please confirm if this is what you are saying

    $users = array();
    $users[] = array("id"=>1,"points"=>267);
    $users[] = array("id"=>2,"points"=>265);
    $users[] = array("id"=>3,"points"=>245);

    $ranks = array();
    $ranks[] = array("rank"=>1,"points"=>345);
    $ranks[] = array("rank"=>2,"points"=>267);
    $ranks[] = array("rank"=>3,"points"=>265);

    $final = array();
    foreach($users as $key => $user)
    {
        $user['rank'] = 0 ; // Not rancked Yet
        foreach ($ranks as $rKey => $rank)
        {    
            //var_dump($rank);
            if($user['points'] >= $rank['points'])
            {
                $user['rank'] = $rank['rank'] ;
            }
        }

        $final[$key] = $user ;
    }

    var_dump($users,$final);

Output

    // Users Before 
    array
      0 => 
        array
          'id' => int 1
          'points' => int 267
      1 => 
        array
          'id' => int 2
          'points' => int 265
      2 => 
        array
          'id' => int 3
          'points' => int 245


     // Users After        
    array
      0 => 
        array
          'id' => int 1
          'points' => int 267
          'rank' => int 3
      1 => 
        array
          'id' => int 2
          'points' => int 265
          'rank' => int 3
      2 => 
        array
          'id' => int 3
          'points' => int 245
          'rank' => int 0

Thanks :)

Upvotes: 0

liquorvicar
liquorvicar

Reputation: 6106

What I would do is to sort your array of current points so it;s ordered by rank and then loop through each array adding the new points to the current points. See here for how to sort a multi-dimensional array

So

$currentPoints = /* sort current points array */
foreach( $rankingPoints as $rank => $extraPoints ) {
  // probably add some sanity checking here to make sure this rank exists in the $currentPoints array
  $currentPoints[$rank]['points']+= $extraPoints;
}

$currentPoints should now contain the updated points totals.

With a little work you could probably make each array a simple single-dimensional array which would make sorting etc easier.

Upvotes: 0

Related Questions