PHP数组转换为Javascript数组 [英] PHP array into Javascript Array
本文介绍了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); ?>;
这篇关于PHP数组转换为Javascript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文