<script type="text/javascript">
var mapArray = [];
var mapObject = {};
</script>
<?php while(have_rows('package_map')){
the_row();
$location = get_sub_field('package_map_points');
?>
<script type="text/javascript">
mapObject.lat = "<?= echo $location['lat'] ?>";
mapObject.lng = "<?= echo $location['lng'] ?>";
mapArray.push(mapObject);
</script>
<?php } ?>
输出应为:[{'lat':34234.23,'lng':32432342.13},{'lat': 23234.23,'lng':635435.13}]
我试过但它只在我控制它时才返回第一条记录,有什么方法可以实现这个目标!
答案 0 :(得分:0)
你不应该混合使用PHP和Javascript,最好在PHP中准备数据并将其“传递”给Javascript:
<?php
$locations = array();
while (have_rows('package_map')) {
the_row();
$locations[] = get_sub_field('package_map_points');
}
?>
<script type="text/javascript">
var locations = <?php echo json_encode($locations) ?>;
</script>