stefffff Posted March 3, 2009 Share Posted March 3, 2009 how can i use a javascript variable in php or is it possible to transform a php array in a javascript array? let's say i have the following dynamically generated php array : $r = array('p1.jpg,p2.jpg,p3.jpg'); meaning that $r[0] = 'p1.jpg'; $r[1] = 'p2.jpg'; $r[2] = 'p3.jpg'; how can i transform this array to be used in javascript? i want the javascript array to get the php array values.... and get something like this... is there a way? var r = array(); r[0] = 'p1.jpg' r[1] = 'p2.jpg' r[2] = 'p3.jpg' Link to comment https://forums.phpfreaks.com/topic/147745-transform-php-array-in-a-javascript-array-can-it-be-done-or-not/ Share on other sites More sharing options...
trq Posted March 3, 2009 Share Posted March 3, 2009 Firstly you need a valid php array, then simply loop through it and right it into a javascript array. <?php $r = array('p1.jpg','p2.jpg','p3.jpg'); echo "<script type=\"text/javascript\">"; echo "var r = new Array();"; foreach ($r as $k => $v) { echo "r[$k] = \"$v\""; } echo "</script>"; ?> Link to comment https://forums.phpfreaks.com/topic/147745-transform-php-array-in-a-javascript-array-can-it-be-done-or-not/#findComment-775559 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.