mikesta707 Posted August 3, 2009 Share Posted August 3, 2009 Hello all. What I am trying to do is echo a 2-D php variable in javascript. So far, I have something like this element.innerHTML = <?php echo count($full_array); ?>; for (i = 0; i < <?php echo count($full_array) ?>; i++){ string += "show:" + "<?php echo $full_array[0][5]; ?>" + "<br />"; } and this works perfectly fine. What I want to do is iterate through the entire PHP array, and output all the different entries. Normally, in php you just do something like for($i = 0; $i < count($array); $i++){ //do whatever like: echo $array[$i]; } or use the foreach loop. My problem is that since I'm looping through in Javascript, I can't use $i variable, and I need another way of telling PhP which specific entry in the array I want. I'm sure there is a simple solution to this, but I can't seem to figure it out. Anyone got any insight? using php 5.2 btw Quote Link to comment https://forums.phpfreaks.com/topic/168662-solved-outputting-a-php-array-in-javascript/ Share on other sites More sharing options...
.josh Posted August 3, 2009 Share Posted August 3, 2009 php is server-side. It executes the code and sends results to browser. So by the time javascript gets the value(s) like you have before, it's just plain text. In other words, you cannot loop through a php array using javascript. Okay, technically you can, if you were to use ajax and a session array or something, but I think the simpler solution is to use a php loop to build and output a javascript array and then loop through the javascript array in your js code. Quote Link to comment https://forums.phpfreaks.com/topic/168662-solved-outputting-a-php-array-in-javascript/#findComment-889752 Share on other sites More sharing options...
watsmyname Posted August 3, 2009 Share Posted August 3, 2009 well i think you can do like this create a php variable, explode the php array with ^ so that new variable carries whole your array data as string, and in javascript create a javascript variable and split the variable you got from php and hence you will get javascript array and you can do whatever you want after you get javascript array Quote Link to comment https://forums.phpfreaks.com/topic/168662-solved-outputting-a-php-array-in-javascript/#findComment-889755 Share on other sites More sharing options...
mikesta707 Posted August 3, 2009 Author Share Posted August 3, 2009 php is server-side. It executes the code and sends results to browser. So by the time javascript gets the value(s) like you have before, it's just plain text. In other words, you cannot loop through a php array using javascript. Okay, technically you can, if you were to use ajax and a session array or something, but I think the simpler solution is to use a php loop to build and output a javascript array and then loop through the javascript array in your js code. Yeah, something like this is what I was thinking. I think I got it working. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/168662-solved-outputting-a-php-array-in-javascript/#findComment-889759 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.