473,513 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to insert an array into an array?

194 New Member
Hi everyone,

I am not very much familiar while working with arrays. I have a situation where i have to add an array into an array. the code without loop is:
Expand|Select|Wrap|Line Numbers
  1. $value =  array(array('Ottawa, Canada', 'Ottawa'), array('Montreal, Canada', 'Montreal'));
  2.  
Now im trying to use it with while loop something like this

Expand|Select|Wrap|Line Numbers
  1. while($rs = mysql_fetch_assoc($qry))
  2. {
  3.     $add_addresses[] = array($rs['address'], $rs['country']);
  4. }
  5.  
This is failing me because its showing a single array whereas i need arrays inside an array. Please guide me how i can achieve this.

thank you.
Jan 19 '11 #1
11 2171
code green
1,726 Recognized Expert Top Contributor
You mean a 2d array? A PHP 2d array is actually an array inside an array, and not a 2d matrix as in C, so this can be done
Although I am struggling to understand what you are trying to do.
This is failing me because its showing a single array
A concise desrciption of your aim is more helpful than a vague 'This is failing me..'
Jan 19 '11 #2
mfaisalwarraich
194 New Member
thank you for ur reply. wat im trying to do is saving each row of a table into separate array in such way that each array holds a separate value of address inside a while loop.

like i have 2 records inside a mysql table. now im trying to fetch its records using while loop

Expand|Select|Wrap|Line Numbers
  1. while($rs = mysql_fetch_assoc($qry))
  2. {
  3.     echo $value = $rs['address'],'-',$rs['country'];
  4. }
  5.  
The above working fine. Now i want to make each row of $rs as an array for example if i have 2 records there will be 2 $value arrays holding different values.

All this is required cuz im trying to make google map. Here is the code
Expand|Select|Wrap|Line Numbers
  1. for ($i=0; $i<count($points); $i++) {
  2.   $api->addGeoPoint($points[$i][0],$points[$i][1], $points[$i][2] , true);
  3. }
  4. $addresses = array(array('Montreal, Canada', 'Canada'), array('Ottawa, Canada', 'Canada'),array('Sydney, Australia', 'Australia'),array('Norway, Norway', 'Norway'));
  5.  
  6. // add the addresses
  7. for ($i=0; $i<count($addresses); $i++) {
  8.   $api->addAddress($addresses[$i][0], $addresses[$i][1], true);
  9. }
  10.  
Notice $addresses array which is here hard coded with different arrays holding address values. But im trying to get that values from a database table by way of making each record as a separate array. i have downloaded a class from phpclasses which im using here. i hope now u will get idea wat im trying to do here and will guide me accordingly.

thank u.
Jan 19 '11 #3
code green
1,726 Recognized Expert Top Contributor
$rs = mysql_fetch_assoc($qry) actually returns an array with the field names as keys.
If you want all the rows saved into a 2d array then
Expand|Select|Wrap|Line Numbers
  1. while($rs = mysql_fetch_assoc($qry)) 
  2.     $add_addresses[] = $rs; 
  3.  
I can't see where the result is being used in the google map code
Jan 19 '11 #4
mfaisalwarraich
194 New Member
as i mentioned there is a class name nxgooglemapsapi.php available at phpclasses dot org. im trying to work with this class. here is the complete call code
Expand|Select|Wrap|Line Numbers
  1. $api = new NXGoogleMapsAPI();
  2.  
  3. // setup the visual design of the control
  4. $api->setWidth(465);
  5. $api->setHeight(312);
  6. $api->setZoomFactor(1);
  7. $api->addControl(GSmallMapControl);
  8.  
  9. // define points
  10. //$points = array(array(50,10,'Point1'), array(51,15, 'Point2'));
  11.  
  12. // add a point. 
  13. for ($i=0; $i<count($points); $i++) {
  14.   $api->addGeoPoint($points[$i][0],$points[$i][1], $points[$i][2] , true);
  15. }
  16. $qry = mysql_query("select address,country from tbl");
  17.  
  18. while($rs = mysql_fetch_assoc($qry))
  19. {
  20.     $add_addresses[] = $rs;
  21. }
  22. //this not working
  23. $addresses = $add_addresses;
  24.  
  25. //this is working
  26. //$addresses = array(array('Ottawa, Canada', 'Canada'),array('Sydney, Australia', 'Australia'),array('Norway, Norway', 'Norway'));
  27.  
  28. // add the addresses
  29. for ($i=0; $i<count($addresses); $i++) {
  30.   $api->addAddress($addresses[$i][0], $addresses[$i][1], true);
  31. }
  32.  
  33.  echo $api->getHeadCode(); 
  34.  
  35. echo $api->getBodyCode();
  36. ?>
  37.  
I tried wat u suggested but its still not working.
Jan 19 '11 #5
code green
1,726 Recognized Expert Top Contributor
You are still replying with vague 'this not working', instead of better explaining your aim.
Are you saying that you want the result from the query
Expand|Select|Wrap|Line Numbers
  1. while($rs = mysql_fetch_assoc($qry)) 
  2.     $add_addresses[] = $rs; 
  3.  
Manipulating into the format
Expand|Select|Wrap|Line Numbers
  1. array(array('Ottawa, Canada', 'Canada'),
  2. array('Sydney, Australia', 'Australia'),
  3. array('Norway, Norway', 'Norway')....)
Jan 20 '11 #6
mfaisalwarraich
194 New Member
yes i want to manipulate it result of query into the following format:

Expand|Select|Wrap|Line Numbers
  1. array(array('Ottawa, Canada', 'Canada'),
  2. array('Sydney, Australia', 'Australia'),
  3. array('Norway, Norway', 'Norway')....)
  4.  
Jan 20 '11 #7
code green
1,726 Recognized Expert Top Contributor
OK. And what do the query results look like?
That is the contents of each $rs from
Expand|Select|Wrap|Line Numbers
  1. while($rs = mysql_fetch_assoc($qry))
If you have commas in the address field that will cause problems now and in the future.
Jan 20 '11 #8
mfaisalwarraich
194 New Member
please test the code i provided with the class im working with. it will give u better idea wat is exactly going on. i tried $rs contents but its failing me.

thank you.
Jan 20 '11 #9
code green
1,726 Recognized Expert Top Contributor
please test the code i provided with the class im working with
No thanks. What does the data returned from your query look like?
I can't advise on how to manipulate into the required format unless I know what it looks like originally.
Jan 20 '11 #10
mfaisalwarraich
194 New Member
Expand|Select|Wrap|Line Numbers
  1. while($rs = mysql_fetch_assoc($qry))
  2. {
  3.     $add_addresses[] = array(array($rs['address'], $rs['country']));
  4. }
  5. print_r($add_addresses);
  6.  
result:
Expand|Select|Wrap|Line Numbers
  1. Array ( [0] => Array ( [0] => Array ( [0] => Ottawa,Canada [1] => Canada ) ) [1] => Array ( [0] => Array ( [0] => Sydney, Australia [1] => Australia ) ) )
  2.  
Jan 20 '11 #11
mfaisalwarraich
194 New Member
okie i got it :) removed this:

Expand|Select|Wrap|Line Numbers
  1.  $add_addresses[] = array(array($rs['address'], $rs['country']));
  2.  
with this one
Expand|Select|Wrap|Line Numbers
  1.  $add_addresses[] = array($rs['address'], $rs['country']);
  2.  
and it worked. thank you for your help CG.
Jan 20 '11 #12

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

Similar topics

2
2757
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
575
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
5156
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
3
1848
by: Nicolas Fleury | last post by:
Hi, Does anyone know if arrays would be picklable in python 2.4? Until then, I tried to derive from array.array and add __setstate__ and __getstate__ with the following code, but it seems I'm not...
2
2043
by: Gus Tabares | last post by:
Hello all, I'm trying to subclass array.array but having problems with a default parameter in the init constructor. Example: import array class TestClass(array.array): def __init__(self,...
0
2818
by: Chris Lambacher | last post by:
Hi, I have to do some data manipulation that needs to be fast. I had a generator approach (that was faster than a list approch) which was taking approximately 5 seconds to run both encode and...
8
10199
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
2
1466
by: John Machin | last post by:
Googling for "pickle array" in comp.lang.python yields old messages that show a PickleError -- plus one message where Alex Martelli writes "I am but an egg" :O) Looks like arrays are NOW (2.4.1)...
17
7211
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
1
2291
by: Fabio | last post by:
Hi All, I have a question concerning the use of array.array inside of C++ code I wrote. I am working with _big_ data files but some entries in these files are usually bounded say between -5 to...
0
7260
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
7160
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...
0
7384
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,...
0
5685
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,...
1
5086
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...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1594
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 ...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
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...

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.