Jump to content

Putting a PHP Array in a JavaScript Array


beennn
 Share

Recommended Posts

I'm having some trouble putting a php array into a javascript array, here's what I have:

<?php  $table = array(); while($d = mysql_fetch_array($sDP)) {$table[] = $d;  } ?>   <script Language="JavaScript"> var description = new Array; <?php for($i=0; $i < 7; $i++) { echo "description[$i]='".$table[$i]."';\n";} ?> for(i = 0; i < 7; i++){   document.write(description[i])  }  </SCRIPT>

However document.write gives out "Array", though I'm not sure where it's coming from. I know it's not coming from my database, so there must be something wrong with the way I'm assigning the array?

Link to comment
Share on other sites

json_encode() is the best option. The reason your first script isn't working is because $table is a two-dimensional array and therefore requires two nested loops to read values from it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...