PHP recordset to array

hi i have a query that returns several  lat and long I want to send to an array and then to a map. How can I covert this to an array?
NGTLDAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roger BaklundCommented:
Are you using mysql? The mysql_fetch_assoc() function creates an associative array of each row. To make an array of arrays, you can do something like this:
$res = mysql_query('select lat,long from MyTable');
if(!$res) die(mysql_error());
$rows = array();     # create empty array
while($row = mysql_fetch_assoc($res))  # fetch rows
  $rows[] = $row;    # append row to array
SendToMap($rows);    # send array to map

Open in new window

0
NGTLDAuthor Commented:
I am using mysql I am trying this now
0
NGTLDAuthor Commented:
I tried it and the out put is simply "Array" what did I miss?
BTW I know the send to map is missing. I tried and echo command to see what was being output because the map was not displaying data
mysql_select_db(REMOVED);
$res = mysql_query('SELECT CONCAT(latitude," , ", longitude) as myloc FROM Geocoord');
if(!$res) die(mysql_error());
$rows = array();     # create empty array
while($row = mysql_fetch_assoc($res))  # fetch rows
  $rows[] = $row;    # append row to array

Open in new window

0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Roger BaklundCommented:
You can not echo an array, but you can use var_dump():

var_dump($rows);

If you are going to use this with javascript, you can use json_encode():

echo json_encode($rows);

http://php.net/manual/en/function.json-encode.php
0
NGTLDAuthor Commented:
I am using a javascript, basically I want to feed the array into the javascript.
0
NerdsOfTechTechnology ScientistCommented:
PHP array derived from mySQL converted into javascript array

by:
=NerdsOfTech

myArray will now be a numerically indexed multidimensional javascript array
mysql_select_db(REMOVED);
$res = mysql_query('SELECT CONCAT(latitude," , ", longitude) as myloc FROM Geocoord');
if(!$res) die(mysql_error());
echo ='<script language="JavaScript">';
echo 'var myArray = new array();';
$i=0;
while($row = mysql_fetch_assoc($res)) {
 $j=0;
 echo 'myArray[' . $i . '] = new array();';
 foreach ($row as $value){
  echo 'myArray[' . $i . '][' . $j . '] = "' . $value . '";';
 }
}
echo ='</script>';

Open in new window

0
Roger BaklundCommented:
Using json_encode(), assuming you have a function "MapFunction()" which needs a list of objects with a 'myloc' property:
mysql_select_db(REMOVED);
$res = mysql_query('SELECT CONCAT(latitude," , ", longitude) as myloc FROM Geocoord');
if(!$res) die(mysql_error());
$rows = array();     # create empty array
while($row = mysql_fetch_assoc($res))  # fetch rows
  $rows[] = $row;    # append row to array
# Write javascript
echo '<script type="text/javascript">'."\n";
echo 'var Arr='.json_encode($arr).";\n";
echo 'MapFunction(Arr);'."\n";
echo '</script>'."\n";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NGTLDAuthor Commented:
THanks man great stuff
0
NerdsOfTechTechnology ScientistCommented:
Thanks for the new knowledge of json_encode() !
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.