query-6ren">
gpt4 book ai didi

javascript - php 数组(数组)到 javascript

转载 作者:行者123 更新时间:2023-12-01 00:42:18 28 4
gpt4 key购买 nike

我有一个包含 GPS 坐标的 MySQL 数据库。这是我检索坐标的部分 PHP 代码;

$sql = "SELECT lat, lon FROM gps_data";
$stmt=$db->query($sql);
$result=$stmt->fetchAll();

现在我需要将返回的 PHP 数组“转换”为 javascript 中的数组数组。我已经试过了;

var js_var = <?php echo json_encode($result); ?>;

但这不是想要的输出

当前输出:

var js_var = [{"lat":"61.883350","0":"61.883350","lon":"8.551115","1":"8.551115"},{"lat":"61.883380","0":"61.883350","lon":"8.551715","1":"8.551715"}];

期望的输出:

var js_var =
[[61.883350,8.551115],[61.883380,8.551715]];

如何实现?

最佳答案

您可以使用 .map , 像这样

var js_var = [{"lat":"61.883350","0":"61.883350","lon":"8.551115","1":"8.551115"},{"lat":"61.883380","0":"61.883350","lon":"8.551715","1":"8.551715"}];

var result = js_var.map(function (el) {
return [el.lat, el.lon];
});

console.log(result);

关于javascript - php 数组(数组)到 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29485085/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com