hukadeeze Posted March 8, 2007 Share Posted March 8, 2007 I'm trying to copy the contents of a numerically indexed php array ($menu) to a javascript array (javascriptmenu). I'm not too familiar with js. The only idea I had was to create an array in js and then set it equal to the php array: var javascriptmenu = new Array() var javascriptmenu = '.$menu.' I'm using the '..' because this block is already contained within single quotes. When I try to alert an item in the array javascriptmenu: alert(javascriptmenu[0]) I get this message: undefined Am I doing something wrong, or is this just not possible? Quote Link to comment https://forums.phpfreaks.com/topic/41854-copy-array-from-php-to-javascript/ Share on other sites More sharing options...
linuxdream Posted March 8, 2007 Share Posted March 8, 2007 It is but you have to be a bit more precise in the definition... <?php $menu = array('apple', 'banana', 'peach'); echo "<script>\n"; echo "var jsmenu = new Array();\n"; foreach($menu as $item){ echo "jsmenu.push($item)\n"; } echo "</script>"; ?> Should work... Quote Link to comment https://forums.phpfreaks.com/topic/41854-copy-array-from-php-to-javascript/#findComment-202961 Share on other sites More sharing options...
mainewoods Posted March 8, 2007 Share Posted March 8, 2007 php can echo out javascript as text so just loop thru the php array and write out javascript as text that will create and assign the js array browser side: <script type="text/javascript"> var javascriptmenu = new Array(); <?php for ($i=0; $i < count($menu); $i++) { echo "javascriptmenu[$i] = '" . $menu[$i] . "';"; //outputting javascript! } ?> </script> Quote Link to comment https://forums.phpfreaks.com/topic/41854-copy-array-from-php-to-javascript/#findComment-202967 Share on other sites More sharing options...
mainewoods Posted March 8, 2007 Share Posted March 8, 2007 that's a nice solution, linuxdream, but I believe you will need a couple of single quotes around $item to work: echo "jsmenu.push('$item')\n"; Quote Link to comment https://forums.phpfreaks.com/topic/41854-copy-array-from-php-to-javascript/#findComment-202969 Share on other sites More sharing options...
linuxdream Posted March 8, 2007 Share Posted March 8, 2007 Cool, thanks. My JS skilz are about a -3 right now. Quote Link to comment https://forums.phpfreaks.com/topic/41854-copy-array-from-php-to-javascript/#findComment-202980 Share on other sites More sharing options...
hukadeeze Posted March 8, 2007 Author Share Posted March 8, 2007 ok, I'll give that a try. just out of curiosity, how does the push function work vs. just using an assignment operator? Will alert(jsmenu[0]) output apple? Quote Link to comment https://forums.phpfreaks.com/topic/41854-copy-array-from-php-to-javascript/#findComment-202982 Share on other sites More sharing options...
hukadeeze Posted March 8, 2007 Author Share Posted March 8, 2007 I ran into a problem. This block of html and js is being returned from a function so it's all inside single quotes (Everything but the loop is just there to show the structure). return ' <html> <script> 'foreach($menu as $item){ echo "jsmenu.push($item)\n"; }' </script> </html> '; When I try to close with ' run the loop and then open again with ' I get an error that says unexpected foreach. Quote Link to comment https://forums.phpfreaks.com/topic/41854-copy-array-from-php-to-javascript/#findComment-203042 Share on other sites More sharing options...
linuxdream Posted March 9, 2007 Share Posted March 9, 2007 Push is used to "push" an element onto the end of an array. I would break up the return info. <?php .... $text = ""; foreach($menu as $item){ $text .= "jsmenu.push($item)\n"; } return $text; //$text will have all the js push items to be echo'd out later ?> Adding whatever tags you need before or after the foreach results in $text. That would work better (and is more readable) than your previous return. Quote Link to comment https://forums.phpfreaks.com/topic/41854-copy-array-from-php-to-javascript/#findComment-203068 Share on other sites More sharing options...
silentg0d Posted March 9, 2007 Share Posted March 9, 2007 thanks i needed this Quote Link to comment https://forums.phpfreaks.com/topic/41854-copy-array-from-php-to-javascript/#findComment-203080 Share on other sites More sharing options...
hukadeeze Posted March 9, 2007 Author Share Posted March 9, 2007 It didn't work. just gave me the string: jsmenu.push(Array).jsmenu.push(Array) Quote Link to comment https://forums.phpfreaks.com/topic/41854-copy-array-from-php-to-javascript/#findComment-203287 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.