Re: array structure as with array_reverse preserve_keys



Bosconian wrote:
I have an array defined as follows:

$scores[2] = 19;
$scores[4] = 25;
$scores[2] = 23;
$scores[4] = 25;

... where the key is the team # and the value is the points.

I am outputting the key/values as follows:

foreach ($scores as $team => $points) {
echo "Team: $team, Points: $points, Difference: " . ($scores[0] -
$scores[1]) . "<br>";
$scores = array_reverse($scores, false);
}

Curent output (before array_reverse):

Game 1
Team: 2, Points: 19, Difference: 0
Team: 4, Points: 25, Difference: 6

Game 2
Team: 2, Points: 23, Difference: 0
Team: 4, Points: 25, Difference: 2


A problem occurs when calculating the point "Difference". The first pass of
the foreach loop (above) is incorrect, but the second pass is correct
(below.) This is due from the use of the function array_reverse(). By
setting the preserve_keys to false, the function changes the structure of
the array.

Desired output (after array_reverse):

Game 1
Team: 2, Points: 19, Difference: -6
Team: 4, Points: 25, Difference: 6

Game 2
Team: 2, Points: 23, Difference: -2
Team: 4, Points: 25, Difference: 2


Question: how can the above array be structured like this from the start?



Well, first of all:

$scores[2] = 19;
$scores[4] = 25;
$scores[2] = 23;
$scores[4] = 25;

doesn't work. You have scores[2] contain both 19 and 23, which you can't do.

Secondly, the line:

echo "Team: $team, Points: $points, Difference: " . ($scores[0] -
$scores[1]) . "<br>";

Would always print the difference between $scores[0] and $scores[1] - that is, -6.

So - what's your real code look like?


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.



Relevant Pages

  • Re: attempting an actual game...
    ... >>> and inflexible by the absurd decision to use a bit array for square ... as then one has 8 bits in which to store a color and a few flags ... Using a 2D int (or, ... > Change direction and you may eventually complete a game. ...
    (comp.games.development.programming.misc)
  • array structure as with array_reverse preserve_keys
    ... I have an array defined as follows: ... I am outputting the key/values as follows: ... Game 1 ... the function changes the structure of ...
    (comp.lang.php)
  • Re: Minestorm - getting to the Wave 14 bug
    ... Vectrex help line told me at the time that the bug after level thirteen ... >> As most people on this newsgroup probably know, the Minestorm game ... the array entry described such details as the types of ...
    (rec.games.vectrex)
  • Need advice on best way to teach VB6 to high School Students.
    ... This is my first year teaching VB6 to High School Students. ... Which game. ... Simplified version of "Battleship" using a 10X10 array and hiding 8 ... Maybe start with buttons then go to image control? ...
    (comp.lang.basic.visual.misc)
  • Re: Need advice on best way to teach VB6 to high School Students.
    ... Which game. ... Simplified version of "Battleship" using a 10X10 array and hiding 8 ... Maybe start with buttons then go to image control? ... This is first time I get to teach programming language. ...
    (comp.lang.basic.visual.misc)