Rootstonian Posted September 10, 2011 Share Posted September 10, 2011 I'm coding a google map script in php/javascript, and I'm having a hell of a time with the address. It seems to not like any spaces for what I can tell (and I'm no php programmer). /* Create an array of address values */ addresses = [ <?php for($i=0; $i < count($dataArray); $i++) { $p = $dataArray[$i]; $p = explode(":", $p); $addr = $p[4]; echo "$addr,\n"; } ?> ]; Here is data file (just one line for testing): 1315414069:41.100908:-81.442699:33:150 North Ave Tallmadge OH:1 Here is "view source" from browser: /* Create an array of address values */ addresses = [ 150 North Ave Tallmadge OH, ]; I'm stumped The map will not display with this. If I change the $addr to another variable in the array, it works fine. Trying to put the address in an info window on the map. TIA, Roots Quote Link to comment https://forums.phpfreaks.com/topic/246853-problem-with-variable/ Share on other sites More sharing options...
mikesta707 Posted September 10, 2011 Share Posted September 10, 2011 well the code produced by that script adds an extra comma to the end (in your case, after the first entry in the javascript array) which makes the produced javascript invalid. You need to remove that trailing comma and newline. The following would probably be better <?php $str = "";//start with empty string for($i=0; $i < count($dataArray); $i++) { $p = $dataArray[$i]; $p = explode(":", $p); $addr = $p[4]; $str .= "$addr,\n";//concatenate instead of echoing } //now we can echo, but before we need to take off the ending 2 characters $str = substr($str, 0, strlen($str)-2); echo $str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/246853-problem-with-variable/#findComment-1267744 Share on other sites More sharing options...
AbraCadaver Posted September 10, 2011 Share Posted September 10, 2011 You also need to quote the array values. I would try this: <?php for($dataArray as $value) { $p = explode(":", $value); $addr[] = "'{$p[4]}'"; } ?> addresses = [<?php echo implode(",", $addr); ?>]; Quote Link to comment https://forums.phpfreaks.com/topic/246853-problem-with-variable/#findComment-1267759 Share on other sites More sharing options...
mikesta707 Posted September 10, 2011 Share Posted September 10, 2011 TO be honest though, there is a function that can translate a php object into json notation. its called json_encode. you can use it like so $arr = array();//empty array for($i=0; $i < count($dataArray); $i++) { $p = $dataArray[$i]; $p = explode(":", $p); $addr = $p[4]; $arr[] =$addr;//push each address onto the array } echo json_encode($arr); Quote Link to comment https://forums.phpfreaks.com/topic/246853-problem-with-variable/#findComment-1267761 Share on other sites More sharing options...
Rootstonian Posted September 10, 2011 Author Share Posted September 10, 2011 You also need to quote the array values. I would try this: Adding the quotes did the trick, irregardless of the comma and newline character. http://i116.photobucket.com/albums/o40/ariesdch/screeny.png Note, if I do a var_dump on $addr, it does not show the comma. Thank you, thank you, thank you!! Quote Link to comment https://forums.phpfreaks.com/topic/246853-problem-with-variable/#findComment-1267771 Share on other sites More sharing options...
Rootstonian Posted September 10, 2011 Author Share Posted September 10, 2011 I cleaned-up my code with ALL of your suggestions...to wit: <from "view source" of web page> /* The addresses array */ addresses = ['150 North Ave Tallmadge OH','33 Main Tallmadge OH','107 SouthEast Tallmadge OH']; Much cleaner now and the code is shorter. I am a programmer, just not PHP....yet. This is just a small part of an Android project I'm working on...I'm sure I'll be back with more questions! LOL Just wanted to thank everyone for the quick responses. Roots Quote Link to comment https://forums.phpfreaks.com/topic/246853-problem-with-variable/#findComment-1267813 Share on other sites More sharing options...
mikesta707 Posted September 10, 2011 Share Posted September 10, 2011 going to mark your topic as solved. glad your question got answered Quote Link to comment https://forums.phpfreaks.com/topic/246853-problem-with-variable/#findComment-1267814 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.