473,804 Members | 2,272 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 4607
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
1675
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
4446
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
4169
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
12839
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
10358
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
9714
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10346
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
10347
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
10090
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...
1
7635
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
5531
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...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
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.