本文介紹了PHP數(shù)組轉(zhuǎn)換成Javascript數(shù)組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
下午.下面的代碼工作得很好,但是,我需要將 php sql 數(shù)組的每一行拉出并放入腳本 var 中.關(guān)于如何編寫可以做到這一點的 while 循環(huán)的任何想法?感謝您的幫助
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); ?>;
這篇關(guān)于PHP數(shù)組轉(zhuǎn)換成Javascript數(shù)組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!