473,748 Members | 6,037 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2064
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*******@attgl obal.net
=============== ===
Apr 18 '06 #2
"Jerry Stuckle" <js*******@attg lobal.net> wrote in message
news:4_******** ************@co mcast.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*******@attgl obal.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,$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!

Apr 18 '06 #4

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

Similar topics

9
17606
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 array_reverse(). Can anyone tell me why?
3
7972
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 first 10 lines. So, I'd like to reduce the overhead it takes to read the file into the array by limiting the number of lines read in - rather than have the entire file read in and then limiting the output to the html table. $visits=...
9
1982
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 = "fishes/5/sound.php"; $sounds = "bears/1/sound.php";
3
2986
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 an array) and I want to sort on Descending. This is displayed using print_r() function. Array
4
1809
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, nultidemensional array which is based on the first array: $dl]]]=$link;
19
3696
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 between the data and counter files //for example, if the 3rd line in the counter file is deleted, //so also must the 3rd line of the data file be deleted. // Each line of the data_file has 4 items: //date|ip_address|user_id|url for ( $i=0; $i <...
2
2015
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 inserted into my mySQL database in the proper fields although it is currently being written to another text file because of the problem I have below. The lines from the mp3 text file will look like this: Al Green - The Supreme Al Green - 01 -...
0
1067
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(), indexOf();last(), first() and others.... so how about implementing these cool methods in your regular PHP array?? No problem, lets extend the ArrayObject and have some fun. Here is the class. class ExtendedArrayObject extends ArrayObject {
2
1474
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
9544
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.