473,405 Members | 2,334 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,405 software developers and data experts.

How to store data in multidimensional array in a loop

Hi there,

I need to store three pieces of data - a result of a SQL query - in a multidimensional 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 4578
code green
1,726 Expert 1GB
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
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 Expert 1GB
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($records as $row)
{
if(!strcmp($country,$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_pop_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
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...
2
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...
3
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...
1
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
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,...
2
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...
5
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
0
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...
0
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,...

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.