PHP数组转换成Javascript数组
下午.下面的代码工作得很好,但是,我需要将 php sql 数组的每一行拉出并放入脚本 var 中.关于如何编写可以做到这一点的 while 循环的任何想法?感谢您的帮助
Afternoon all. The code below works perfectly, however, I need to pull each row of the php sql array out and into the script var. Any ideas on how to write a while loop that could do this? Thanks for any help
var enableDays = ["<?php echo mysql_result($result, 0, 'date'); ?>"];
enableDays.push("<?php echo mysql_result($result, 1, 'date'); ?>");
附加代码::
$rows = array();
while ($row = mysql_fetch_assoc($result))
{
$rows[] = $row;
}
var enableDays = [<?php echo json_encode($rows); ?>];
console.log(enableDays[1]);
推荐答案
您可以使用 mysqli_fetch_assoc
获取行,然后将其编码为 JSON:
You can get the rows using mysqli_fetch_assoc
and then encode it to JSON:
<?php
$rows = array();
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
?>
var enableDays = <?php echo json_encode($rows); ?>;
相关文章