PHP循环到数组到javascript数组

ck

我有这段代码可以从数据库中获取所有数据

while($row = mysqli_fetch_array($query)){                          
$therow1 = $row['r1'];
$therow2 = $row['r2'];
$therow3 = $row['r3'];//get
$therow4 = $row['r4'];//get

}

我如何获取$therow3$therow3作为数组,然后将其作为数组传递给javascript。

javascript数组中的预期输出为:$ the_array

 [
    [-33.890542, 151.274856],
    [-33.923036, 151.259052],
    [-34.028249, 151.157507],
    [-33.80010128657071, 151.28747820854187],
    [-33.950198, 151.259302]
    ];

然后我将在javascript中使用此数组

<script type="text/javascript">
    var locations = $the_array;

</script>
伊斯梅尔·RBOUH

请尝试以下方法:

PHP:

while($row = mysqli_fetch_array($query)){                          
    $therow1 = $row['r1'];
    $therow2 = $row['r2'];
    $therow3 = $row['r3'];//get
    $therow4 = $row['r4'];//get
    $the_array[] = [$therow3, $therow4];
}

Javascript:

<script type="text/javascript">
    var locations = JSON.parse('<?php echo json_encode($the_array); ?>');

</script>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章