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: -
$value = array(array('Ottawa, Canada', 'Ottawa'), array('Montreal, Canada', 'Montreal'));
-
Now im trying to use it with while loop something like this -
while($rs = mysql_fetch_assoc($qry))
-
{
-
$add_addresses[] = array($rs['address'], $rs['country']);
-
}
-
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.
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..'
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 -
while($rs = mysql_fetch_assoc($qry))
-
{
-
echo $value = $rs['address'],'-',$rs['country'];
-
}
-
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 -
for ($i=0; $i<count($points); $i++) {
-
$api->addGeoPoint($points[$i][0],$points[$i][1], $points[$i][2] , true);
-
}
-
$addresses = array(array('Montreal, Canada', 'Canada'), array('Ottawa, Canada', 'Canada'),array('Sydney, Australia', 'Australia'),array('Norway, Norway', 'Norway'));
-
-
// add the addresses
-
for ($i=0; $i<count($addresses); $i++) {
-
$api->addAddress($addresses[$i][0], $addresses[$i][1], true);
-
}
-
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.
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 - while($rs = mysql_fetch_assoc($qry))
-
{
-
$add_addresses[] = $rs;
-
}
-
I can't see where the result is being used in the google map code
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 -
$api = new NXGoogleMapsAPI();
-
-
// setup the visual design of the control
-
$api->setWidth(465);
-
$api->setHeight(312);
-
$api->setZoomFactor(1);
-
$api->addControl(GSmallMapControl);
-
-
// define points
-
//$points = array(array(50,10,'Point1'), array(51,15, 'Point2'));
-
-
// add a point.
-
for ($i=0; $i<count($points); $i++) {
-
$api->addGeoPoint($points[$i][0],$points[$i][1], $points[$i][2] , true);
-
}
-
$qry = mysql_query("select address,country from tbl");
-
-
while($rs = mysql_fetch_assoc($qry))
-
{
-
$add_addresses[] = $rs;
-
}
-
//this not working
-
$addresses = $add_addresses;
-
-
//this is working
-
//$addresses = array(array('Ottawa, Canada', 'Canada'),array('Sydney, Australia', 'Australia'),array('Norway, Norway', 'Norway'));
-
-
// add the addresses
-
for ($i=0; $i<count($addresses); $i++) {
-
$api->addAddress($addresses[$i][0], $addresses[$i][1], true);
-
}
-
-
echo $api->getHeadCode();
-
-
echo $api->getBodyCode();
-
?>
-
I tried wat u suggested but its still not working.
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 - while($rs = mysql_fetch_assoc($qry))
-
{
-
$add_addresses[] = $rs;
-
}
-
Manipulating into the format - array(array('Ottawa, Canada', 'Canada'),
-
array('Sydney, Australia', 'Australia'),
-
array('Norway, Norway', 'Norway')....)
yes i want to manipulate it result of query into the following format: -
array(array('Ottawa, Canada', 'Canada'),
-
array('Sydney, Australia', 'Australia'),
-
array('Norway, Norway', 'Norway')....)
-
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 - while($rs = mysql_fetch_assoc($qry))
If you have commas in the address field that will cause problems now and in the future.
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.
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.
-
while($rs = mysql_fetch_assoc($qry))
-
{
-
$add_addresses[] = array(array($rs['address'], $rs['country']));
-
}
-
print_r($add_addresses);
-
result: -
Array ( [0] => Array ( [0] => Array ( [0] => Ottawa,Canada [1] => Canada ) ) [1] => Array ( [0] => Array ( [0] => Sydney, Australia [1] => Australia ) ) )
-
okie i got it :) removed this: -
$add_addresses[] = array(array($rs['address'], $rs['country']));
-
with this one -
$add_addresses[] = array($rs['address'], $rs['country']);
-
and it worked. thank you for your help CG.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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);
}
|
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...
|
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>
|
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...
|
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,...
| |
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...
|
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...
|
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)...
|
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...
|
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...
|
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,...
| |
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...
|
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,...
|
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: 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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |