473,796 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to store data in multidimensiona l array in a loop

97 New Member
Hi there,

I need to store three pieces of data - a result of a SQL query - in a multidimensiona l array, but don't really succeed. Sometimes it works, but then the output doesn't work accordingly.

My SQL result comes with something like this:
Expand|Select|Wrap|Line Numbers
  1. Africa   |    y2000   |   9430
  2. Africa   |    y2005   |   9678
  3. Europe   |    y2000   |   2314
where there are 6 regions and multiple years.

Last thing I came up with was this for stocking the data in the array:
[PHP]$sum_pop_total_ dataset[] = array($row['reg_name'] => array($row['year'], $row['popt']));[/PHP]

That seemed to work. But then the output function went a bit wrong:
[PHP]while (list($key1) = each ($sum_pop_total _dataset))
{
echo "<br />key1: ".$key1;
while (list($key2, $value) = each ($sum_pop_total _dataset[$key1]))
{
echo "<br />key2: ".$key2." value: ".$value[0];

}
}[/PHP]

[HTML]key1: 0
key2: North America value: y_2005
key1: 1
key2: Europe value: y_2005
key1: 2
key2: Asia + Pacific value: y_2005[/HTML]

So, it seemed that the allocation of the SQL result to the principal variable didn't work correctly. But if I take the [] away from $sum_pop_total_ dataset[]*= ..., than the variable is being re-initialized at each time.

Can someone give me a hint what I should do?

Thanks a lot!
Sep 19 '07 #1
3 4604
code green
1,726 Recognized Expert Top Contributor
Don't quite follow what you are trying to achieve. Is it something like this?
Expand|Select|Wrap|Line Numbers
  1. Array([Africa]=>Array([y2000]=>9430,[y2005]=>9678),[Europe]=>Array([y2000]=>2314,[y2005]=>9000))
Which is an associtive array of 2d associative arrays. A multi-dimensional array if you like.
The terminology is personal preference on how you percieve the structure of an array.
Don't think you can do this in one line without any checking of the values in the current row.
But if you use ORDER BY in your SELECT statemenr it will help you greatly
Expand|Select|Wrap|Line Numbers
  1. ORDER BY country,year
Sep 19 '07 #2
luftikus143
97 New Member
Don't quite follow what you are trying to achieve. Is it something like this?
Expand|Select|Wrap|Line Numbers
  1. Array([Africa]=>Array([y2000]=>9430,[y2005]=>9678),[Europe]=>Array([y2000]=>2314,[y2005]=>9000))
Yepp, this is what I would like to have. But I would like to see that array beeing filled up in a loop.

P.S: The ordering isn't really important.
Sep 19 '07 #3
code green
1,726 Recognized Expert Top Contributor
The ordering isn't really important.
If you ORDER BY country,year then then the loop is something like

[PHP]$sum_pop_total_ dataset = array();
$year_pop = array();
$country = $records[0]['reg_name'];
foreach($record s as $row)
{
if(!strcmp($cou ntry,$row]['reg_name']) #are we still with the same country
$year_pop[$row['year']] = $row['popt'];
else
{
$sum_pop_total_ dataset[$country]=$year_pop; #insert the sub array into main array
$country = $row['reg_name']; #assign the next country
$year_pop[$row['year']] = $row['popt']; #continue
}
}
print_r($sum_po p_total_dataset ); #Lets have a look;[/PHP]
Without ORDER BY more if statements are needed
Sep 19 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1673
by: pauld | last post by:
from a MySQL DB i want to get a multidimensional array that i can loop through either key =field name value = array of ENUM options or array =field name, array= ENUM options and increment x inside loop $q=mysql_query("SHOW FIELDS FROM table" ) or die ("Query failed");
2
4445
by: ben moretti | last post by:
hi i'm learning python, and one area i'd use it for is data management in scientific computing. in the case i've tried i want to reformat a data file from a normalised list to a matrix with some sorted columns. to do this at the moment i am using perl, which is very easy to do, and i want to see if python is as easy. so, the data i am using is some epiphyte population abundance data for particular sites, and it looks like this:
3
305
by: TLOlczyk | last post by:
I have a brain cramp and I need some help. I have a chunk of code below which demonstrates a problem I have with multidimensional arrays. I want to keep it simple but something specific is getting in the way. int a; int b; int **present; int **next;
1
4168
by: @ nospam.com | last post by:
Hi, If Im using Jagged arrays, the following is possible: int arr = new int for(int i=0; i<10; i++) { arr = new int; }
2
12837
by: chris | last post by:
Hi there, I created a Multidimensional array of labels Label lblMultiArray = new Label { {Label3, LblThuTotal}, {Label4,LblFriTotal} }; Now I would like to compare the values in the array, comparing the text in Label3 and LblThuTotal and the text in Label4 and LblFriTotal.
2
1688
by: Allen Maki | last post by:
Hi Everybody, I am new to VC.NET and I need your help. I would like the array to print characters as '.'s (dots) instead of 0s as in (see below in front of //------>>>>>. I tried to use casting like this:
5
10357
by: TS | last post by:
is there some code somewhere that does this? i have a jagged array that is not jagged, it has an equal number of rows and columns in each array so it should convert but want to get the code to do it somewhere. thanks
4
2772
by: taquito | last post by:
Hi, a C newbie here. I have been reading threads here about reading txt files into array. I simply copied a couple of useful codes from here and modified to fit my situations. Then, I have two questions. I have a data set whose data sizes vary. let's say: data A: 100*20 array; data B: 180*20 array; data C: 250*20 array;
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10455
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
10228
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
10173
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
10006
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
9052
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...
0
6788
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
5441
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...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.