0

I have really done what I think is a not-code-review-passing hack to pass a 2-dimensional array from PHP to Javascript. The outer array has elements, and each of its elements is an array.

Each inner array consists of 4 fields from my database records. The inner array elements are: the name of a town, its latitude and its longitude, and an integer index.

Is the code working? Yes. But I'm hating the fact that I am 99% convinced that, as a fairly raw beginner, there is a much better way to create then pass a 2d array from PHP to Javascript and I need to know how, as I hacked this together through trial-and-error and reading lots of SO posts, and did not create this from a priori know-how, wisdom or confidence.

Each outer array element is an array that (needs to) look like this:

top array:

[0] = ["Campbell", 37.21, 122.0, 0]

[1] = ["Sunnyvale", 37.54, 121.37, 1]

[2] = ["Saratoga", 37.24, 122.001, 2]

[3] = ......etc. etc...........

Note that the 2nd level arrays have a string, then a float, then another float, then an integer.

Here's the code in PHP that packages my database records into a 2-d PHP array (error checking, other code is not shown, for clarity):

 $result = mysql_query($query);
 $numrows = mysql_num_rows($result);

 $outerArray = array();

 for($i = 0; $i < $numrows;  $i++)
 {
   $theRow = mysql_fetch_row($result);

   $city = $theRow[1];
   $lat = $theRow[22];
   $lng = $theRow[23];

   $outerArray[] = array($city, $lat, $lng, $i);
 }
 $latLngValues = json_encode($outerArray);

 // EDIT: ADDED TO TELL ME WHAT THIS 2d ARRAY ACTUALLY LOOKS LIKE
 var_dump($latLngValues);

In the onload handler for my web page, I call a javascript function and pass this array:

 <body onload='handleLoad(<?php echo $latLngValues ?>)'>

Here is my handleLoad():

 function handleLoad( latLng2dArray )
 {

   for (var i = 0; i < latLng2dArray.length; i++) 
   {

     var town = latLng2dArray[i];

     var latitude = Number(town[1]);
     var longitude = Number(town[2]);

     // I USE THE DATABASE RECORD'S NUMBERS TO CREATE A Gmaps LatLng
     var myLatLng = new google.maps.LatLng(latitude, longitude);

   }

There's probably a cleaner, and/or more efficient, way to do this.

For example, I found that if I did not use the Number() function to 'force' my database numbers into a number, the call to "new google.maps.LatLng(latitude, longitude)" was giving me NaN.

So I'm hoping more experienced people can let me know a cleaner way. While this code works, I hacked at it for a day through trial and error.

EDIT: I used a var_dump() in my php code to see what the 2d array "$latLngValues" looks like just before it's passed to my onload handler, and "$latLngValues" looks like this:

  string(133) "[["Campbell","37.2724","-122",0],["Sunnyvale","37.2724","-122",1],["LosGatos","37.2607","-122",2],["Saratoga","37.2607","-122.046",3]]" 

I want to point out this: in my database, the data type I used is 'float' for the latitude and longitude. Not sure how/why the var_dump shows them as a string (not floats) while the integer index is treated correctly, and not a string.

6
  • What does $latLngValues actually contain? Have you inspected it? Maybe when you do, the cause will be obvious. Commented Mar 8, 2014 at 21:18
  • @GolezTrol okay, thanks for the suggestion, I added a call to var_dump in my Php code to tell me what latLngValues array looks like, and I added that to my post above. Commented Mar 8, 2014 at 21:26
  • All your numbers are strings. It somehow doesn't look right. Maybe you may consider casting $lat and $lng before adding them to the array? Commented Mar 8, 2014 at 21:28
  • @Alexander Okay thanks, I am not sure how to cast from string to float, in C it would just be putting (float) in front of the variable. By the way, the 'float' data type is used in the database for the latitude and longitude, I don't know why the var_dump shows them as strings and yet var_dump shows the final array element -- an index value-- correctly as a number. Commented Mar 8, 2014 at 21:32
  • Looking at the documentation of mysql_fetch_row, it seems to return all strings Commented Mar 8, 2014 at 21:33

1 Answer 1

0

No one offered a better coding strategy so for now, I'm going with the existing code above -- it aint pretty but it works. I'll post back if a more-correct way is found.

1
  • There's no way around mysql_fetch_row returning only strings. You need to cast numeric values as said above. I don't see a problem with json_encode though, that's fine Commented Mar 9, 2014 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.