Hi, Here is a working code to copy 2d php array to 2d javascript array. <html> <head> <?php for($i = 0; $i < 3; $i++) { for($j = 0; $j < 2; $j++) {$quest[$i][$j] = $i*10+$j;} } ?> <script type="text/javascript"> var questions = new Array(3); for (var i = 0; i < 3; i++) { questions[i] = new Array(2); } questions[0] = ["<?php echo join("\", \"", $quest[0]); ?>"]; questions[1] = ["<?php echo join("\", \"", $quest[1]); ?>"]; questions[2] = ["<?php echo join("\", \"", $quest[2]); ?>"]; document.write(questions[0][0] + "<br />"); document.write(questions[0][1] + "<br />"); document.write(questions[1][0] + "<br />"); document.write(questions[1][1] + "<br />"); document.write(questions[2][0] + "<br />"); document.write(questions[2][1] + "<br />"); </script> </head> </html> Code (markup): Now,here's the thing.Notice these lines in the code questions[0] = ["<?php echo join("\", \"", $quest[0]); ?>"]; questions[1] = ["<?php echo join("\", \"", $quest[1]); ?>"]; questions[2] = ["<?php echo join("\", \"", $quest[2]); ?>"]; I would like to put these lines in a loop,something like for (var i = 0; i < 3; i++) { questions = ["<?php echo join("\", \"", $quest); ?>"]; } But even after a lot of efforts I am unable to do so,what am I doing wrong?Thanks
Indeed, because this is impossible. PHP runs on the server, while javascript runs in the browser. Since they run in totally different environments, there's no way to directly exchange information between them (which is what you would need to make a loop like that).
But,the thing is I am able to successfully store the php array in the javascript array using the following: questions[0] = ["<?php echo join("\", \"", $quest[0]); ?>"]; questions[1] = ["<?php echo join("\", \"", $quest[1]); ?>"]; questions[2] = ["<?php echo join("\", \"", $quest[2]); ?>"]; Code (markup): I just need to put the above lines in a loop.
If you want to do something like your first example, you need to do it in PHP (PHP generating javascript), not javascript, so try something like this (try changing the $count variable): <html> <head> <?php $count=3; for($i = 0; $i < $count; $i++) { for($j = 0; $j < 2; $j++) {$quest[$i][$j] = $i*10+$j;} } ?> <?php echo '<script type="text/javascript">'; echo "var questions = new Array($count);"; echo "for(var i = 0; i < $count; i++)"; echo '{'; echo ' questions[i] = new Array(2);'; echo '}'; for($i=0; $i<$count; $i+=1) { echo "questions[$i] = [\"" . join("\", \"", $quest[$i]) . "\"];"; } for($i=0; $i<$count; $i+=1) { echo "document.write(questions[$i][0] + \"<br />\");"; echo "document.write(questions[$i][1] + \"<br />\");"; } echo '</script>'; ?> </head> </html> Code (markup):
Hi, I would like to confirm that the method suggest above by clockedout7 indeed works.I was looking for the answer to this question for a long time.Thanks a lot clockedout7. By the way,a suggestion/request to the moderators of the forum.There should be a "thread resolved" button which people can click if their query is solved.This will help the very kind and helpful problem solvers from unnecessary reading threads that have been resolved