array structure as with array_reverse preserve_keys

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bosconian

    array structure as with array_reverse preserve_keys

    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?


  • Jerry Stuckle

    #2
    Re: array structure as with array_reverse preserve_keys

    Bosconian wrote:[color=blue]
    > 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?
    >
    >[/color]

    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@attgl obal.net
    =============== ===

    Comment

    • Bosconian

      #3
      Re: array structure as with array_reverse preserve_keys

      "Jerry Stuckle" <jstucklex@attg lobal.net> wrote in message
      news:4_-dnZ_9ONr4x9nZRV n-jA@comcast.com. ..[color=blue]
      > Bosconian wrote:[color=green]
      > > 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[/color][/color]
      of[color=blue][color=green]
      > > 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[/color][/color]
      of[color=blue][color=green]
      > > 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[/color][/color]
      start?[color=blue][color=green]
      > >
      > >[/color]
      >
      > 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[/color]
      do.[color=blue]
      >
      > 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[/color]
      is, -6.[color=blue]
      >
      > So - what's your real code look like?
      >
      >
      > --
      > =============== ===
      > Remove the "x" from my email address
      > Jerry Stuckle
      > JDS Computer Training Corp.
      > jstucklex@attgl obal.net
      > =============== ===[/color]

      Indeed, $scores[0] and $scores[1] will equal -6 once the elements are
      properly defined, which was the thrust of my dilemma.

      The above example was meant to illustrate the values were being pushed. My
      bad.

      So

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

      resulting in

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

      ANYWAY, I managed to get everything sorted out once my keys and values were
      properly defined.


      Comment

      • kurtis.jensen@gmail.com

        #4
        Re: array structure as with array_reverse preserve_keys

        I'm not sure this is what your looking for but here is what I came up
        with:

        <?php
        $game[1]= "Game 1";
        $game[2]= "Game 2";

        $scores[1]['2'] = 19;// Game One Team 2
        $scores[1]['4'] = 25;// Game One Team 4

        // This could also be written as: $scores[0] array(2 => 19, 4 => 25);

        $scores[2]['2'] = 23;// Game Two Team 2
        $scores[2]['4'] = 25;// Game Two Team 4

        // This is a two dimentional array where the primary key is the game
        number
        // and the second keys are the team # and the value is the points.

        //############### ########
        // Output
        //############### ########

        // walk through each game
        foreach ($game as $Key => $Description){
        echo "<br/>".$Description .":<br/>";

        // turn the $scores[GameNumber] from an associated to a numerical
        array
        // (because we won't always know what the team number is) and
        put
        // the values into $Score1 and $Score2.
        list ($Score1,$Score 2) = array_values($s cores[$Key]);

        // Calculate the difference
        $Diff = abs($Score1-$Score2);

        // walk through each score for the current game
        foreach ($scores[$Key] as $team => $points) {

        // show output for each team
        echo "Team: ".$team.", Points: ".$points." , Difference:
        ".$Diff."<b r>";
        }

        }
        ?>
        -----------------------
        Good Luck!

        Comment

        Working...