I have a 2d (two dimensional,2 dimensional) php array that I want to copy to a 2d javascript array.The 2d php array is $quest[100][6] and the 2d javascript array is questions[100][6] . I have written the javascript code inside the <?php....?> itself using echo "<script language="javascript" type="text/javascript">.......</script>; Now ,inside the javascript,when I try to copy the 2d php array to the 2d javascript array using the following method it works questions[0]= ["<?php echo join("\", \"", $quest[0]); ?>"]; questions[1]= ["<?php echo join("\", \"", $quest[1]); ?>"]; ... and so on However, if I try to do the same using the following method it does not work for (var i= 0; i <= 99; i++) { questions= ["<?php echo join("\", \"", $quest); ?>"]; } Why is that?What mistake am I making?Any help will be deeply appreciated.Thanks -------------------------------------THE CODE---------------------------------------------- <?php Access database and store result of mysq_query in $result....... $result = mysql_query($query); for ( $count = 0; $count <= 99; $count++) { $quest[$count]=mysql_fetch_array($result,MYSQL_NUM);; } echo "<script language="javascript" type="text/javascript"> var questions = new Array(100); for (var i = 0; i <100; i++) { questions = new Array(6); } /*The following method of copying 2d php array to 2d javascript array is not working for ( var i = 0; i <= 99; i++) { questions= ["<?php echo join("\", \"", $quest); ?>"]; } */ /*The following method ,however,is working*/ questions[0]= ["<?php echo join("\", \"", $quest[0]); ?>"]; questions[1] = ["<?php echo join("\", \"",$quest[1]); ?>"]; questions[2] = ["<?php echo join("\", \"",$quest[2]); ?>"]; ....and so on </script>"; mysql_close($db_server); ?>
I don't know about your code above, but you should be using single quotes, so change your echo from echo "<script language="javascript" type="text/javascript"> //and this </script>"; PHP: to this echo [COLOR="Red"][B]'[/B][/COLOR]<script language="javascript" type="text/javascript"> //and this </script>[COLOR="Red"][B]'[/B][/COLOR]; Code (markup):
Hi, In case,it is difficult to figure out the mistake with my code,can someone tell me,any other method to copy a 2d php array to a 2d javascript array.Thanks
Here's my approach: <?php // view in plain text header("Content-Type: text/plain"); // create a 2d array with random numbers in it for($i = 0; $i < 10; $i++) { for($j = 0; $j < 10; $j++) $arr[$i][$j] = rand( ); } // write js $js_arr = "<script type=\"text/javascript\">\nvar arr = new Array( );\n"; for($i = 0; $i < 10; $i++) { // write a 2d js array declaration $js_arr .= "arr[$i] = new Array( );\n"; for($j = 0; $j < 10; $j++) // write a js array $js_arr .= "arr[$i][$j] = \"" . $arr[$i][$j] . "\";\n"; // this is the general idea } $js_arr .= "</script>"; echo $js_arr; ?> PHP:
Rainulf your approach was quite nice. here is how I would go about it: Simply change $array to the variable of the array. <? header("Content-Type: text/plain"); if($array == null){ echo'<script type="text/javascript"> var arr = new Array( ); '; } //some stuff will need to be changed $data = $array; //change array to the variable of the array //now we find array size $amt = count($data); $amt2 = count($data[0]); $amt3 = count($data[1]); $i = 0; $k = 0; while($i < $amt) { $j = 0; while($j < $amt2){ echo"arr[".$i."][".$j."]=\"".$data[$i][$j]."\";\n"; $j++; } $i++; } ?> </script> PHP: