如何将php数组值分配给javascript数组 [英] how to assign php array values to javascript array
问题描述
请提供有关如何将php数组值分配给javascript数组的帮助
Please provide help on how to assign php array values to javascript array
-----------使用下面的php代码我存储数据在php数组中-----------
----------- Using below php code I am storing data in php array -----------
<?php
$str_query="SELECT title,description FROM tablename ORDER BY title";
$rs_sch=GetRecordset($str_query);
$int_count=0;
$schd_arr = array();
while(!$rs_sch->EOF())
{
$schd_arr[$int_count] = $rs_sch->Fields("title").": ".$rs_sch->Fields("description");
$rs_sch->MoveNext();
$int_count++;
}
?>
-----使用下面的javascript代码我试图将php数组数据存储到javascript数组中 - ----
----- Using below javascript code I am trying to store php array data into javascript array -----
请让我知道在下面提到的2个位置写入变量的内容和方式,以便我的代码可以正常工作。
Please let me know what and how to write variable at below 2 mentioned locations so my code can work.
<script type="text/javascript" language="javascript">
var pausecontent=new Array()
for (sch_cnt=0; sch_cnt<*Here I want to assign value of $int_count php variable*; sch_cnt++)
{
pausecontent[sch_cnt]=<?php *Here I want to assign php array and counter values (something like this - $schd_arr[sch_cnt];)* ?>;
}
</script>
提前谢谢,
KRA
Thank you in advance, KRA
推荐答案
你不能这样循环,你需要循环PHP数组并将 push
循环到javascript数组中:
You can't loop like that, you need to loop the PHP array and push
into javascript array:
<script type="text/javascript" language="javascript">
var pausecontent = new Array();
<?php foreach($schd_arr as $key => $val){ ?>
pausecontent.push('<?php echo $val; ?>');
<?php } ?>
</script>
这篇关于如何将php数组值分配给javascript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!