Jump to content

Recommended Posts

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

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.

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

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.