473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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?
Apr 18 '06 #1
3 2048
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.
js*******@attglobal.net
==================
Apr 18 '06 #2
"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:4_********************@comcast.com...
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.
js*******@attglobal.net
==================


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.
Apr 18 '06 #3
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,$Score2) = array_values($scores[$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."<br>";
}

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

Apr 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: lawrence | last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by the second field in each row? Also, when I use sort() on a two dimensional array, it seems to work a lot like...
3
by: deko | last post by:
It's nice to be able to generate an html table from a PHP array. I know how to do this, but the array in question is built from a file. The file in question can be very long, and I only want the...
9
by: mark | last post by:
I have an array as follows $sounds = array(); $sounds = "fishes/1/sound.php"; $sounds = "fishes/2/sound.php"; $sounds = "fishes/3/sound.php"; $sounds = "fishes/4/sound.php"; $sounds =...
3
by: Paul Kirby | last post by:
Hello All I am trying to update me code to use arrays to store a group of information and I have come up with a problem sorting the multiple array :( Array trying to sort: (7 arrays put into...
4
by: Red | last post by:
I have an array which is dynamically generated by parsing a web page: titles Array ( => bookmarks => New Folder => New Folder2 ) and I want to insert html links into another,...
19
by: deko | last post by:
I'm kind of lost on this one - I need to modify 2 files based on user input: $data_array = file($data_file); $counter_array = file($counter_file); // There is a line-for-line relationship...
2
by: JackM | last post by:
Let me attempt to explain my problem. I have a crude php script that takes a text list of songs that was generated by an mp3 list program and translates each entry into the form where they can be...
0
by: Hasin Hayder | last post by:
You know that in ruby/prototype you can traverse thru each element of array like this Array.each(function(){/*function body*/}). It has also some methods like without(), inspect(),...
2
by: Miles | last post by:
Hi all, Wondering if anyone can help me. If i have an associative array: $arr = array( "one" =array(1, 2, 3), "two" =array(5, 6), "three" =array(7,8,9,10) .... "n" =array(p,q,r....)
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.