我在php中有一个数组($ coords),其中包含一些坐标。
print_r($coords)
给出
Array ( [0] => 80.23338342111089 ,26.52748985468579 [1] => 80.21915925983853 ,26.51065783417543 [2] => 80.23036859334592 ,26.49785585262899 [3] => 80.24667293123426 ,26.51075259323626 [4] => 80.23338342111089 ,26.52748985468579 )
然后我使用了$string = implode(",",$coords);
print_r($string);
它给了我
80.23338342111089 ,26.52748985468579,80.21915925983853 ,26.51065783417543,80.23036859334592 ,26.49785585262899,80.24667293123426 ,26.51075259323626,80.23338342111089 ,26.52748985468579
我将此传递给Javascript,就像这样 -
var Coordinates = [];
Coordinates.push(<?php echo ($string); ?>);
现在我想将其字符串化,所以我使用了 -
var JSON_Coordinates = JSON.stringify(Coordinates);
现在JSON_Coordinates的值是 -
[80.23338342111089 ,26.52748985468579,80.21915925983853 ,26.51065783417543,80.23036859334592 ,26.49785585262899,80.24667293123426 ,26.51075259323626,80.23338342111089 ,26.52748985468579]
但它应该是 -
[[80.23338342111089 ,26.52748985468579],[80.21915925983853 ,26.51065783417543],[80.23036859334592 ,26.49785585262899],[80.24667293123426 ,26.51075259323626],[80.23338342111089 ,26.52748985468579]]
有人请告诉我我哪里错了?
感谢。
答案 0 :(得分:1)
使用array_chunk将数组拆分为
$arr = [80.23338342111089 ,26.52748985468579,80.21915925983853 ,26.51065783417543,80.23036859334592 ,26.49785585262899,80.24667293123426 ,26.51075259323626,80.23338342111089 ,26.52748985468579];
?>
var Coordinates = <?= json_encode(array_chunk($arr,2)) ?>;
// var Coordinates = [[80.233383421111,26.527489854686],[80.219159259839,26.510657834175],[80.230368593346,26.497855852629],[80.246672931234,26.510752593236],[80.233383421111,26.527489854686]];
答案 1 :(得分:0)
需要了解如何在PHP代码中建立数组。它们是成对出现还是全部放在一个大阵列中?
非常简单。只是与您要转换为JSON的数组的结构有关。
$foo = array(1, 2, 3, 4);
// [1,2,3,4]
$bar = array(array(1, 2), array(3, 4));
// [[1,2],[3,4]]
我清理数据服务器端,所以你只需要对其进行JSON编码,然后通过该客户端。
答案 2 :(得分:0)
您应该像这样编写JS逻辑。此时,您在代码中什么都不做,将创建所需的2d数组。
你应该循环遍历整个数组并将计数器递增2以一次获得2个元素并创建一个新的临时数组,该数组将推入原始的arrCoordinates。
协调供应为数组
var coordinates = [80.23338342111089 ,26.52748985468579,80.21915925983853 ,26.51065783417543,80.23036859334592 ,26.49785585262899,80.24667293123426 ,26.51075259323626,80.23338342111089 ,26.52748985468579];
var arrCoordinates =[];
for(var index=0; index< coordinates.length; index+=2){
arrCoordinates.push([coordinates[index], coordinates[index+1]]);
}
console.log(arrCoordinates);
&#13;
将供应协调为字符串
var coordinates = "80.23338342111089 ,26.52748985468579,80.21915925983853 ,26.51065783417543,80.23036859334592 ,26.49785585262899,80.24667293123426 ,26.51075259323626,80.23338342111089 ,26.52748985468579";
var arrCoordinates =[];
var inputCoordinates = coordinates.split(",");
for(var index=0; index< inputCoordinates.length; index+=2){
arrCoordinates.push([inputCoordinates[index].trim(), inputCoordinates[index+1].trim()]);
}
console.log(arrCoordinates);
&#13;
答案 3 :(得分:0)
获得坐标数组后,可以循环遍历它以构建所需的输出。
另一种方法是使用for-loop
。
var coordinates = [80.23338342111089 ,26.52748985468579,80.21915925983853 ,26.51065783417543,80.23036859334592 ,26.49785585262899,80.24667293123426 ,26.51075259323626,80.23338342111089 ,26.52748985468579];
var newCoordinates = [];
for (var i = 0; i < coordinates.length; i++) {
if (i % 2 !== 0) newCoordinates.push([coordinates[i - 1], coordinates[i]]);
}
console.log(newCoordinates);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
根据您的初始数组,您可以循环,拆分然后构建嵌套数组:
初始输入数据: ["c1, c2", "c1, c2", ...]
这样,您就不需要调用函数stringify
。
var array = ["80.23338342111089 ,26.52748985468579", "80.21915925983853 ,26.51065783417543","80.23036859334592 ,26.49785585262899", "80.24667293123426 ,26.51075259323626","80.23338342111089 ,26.52748985468579" ];
var result = [];
for(var i = 0; i < array.length; i++) {
var split = array[i].split(',');
var lt = split[0].trim();
var ln = split[1].trim();
result.push([+lt.trim(), +ln.trim()])
}
console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;