-1

我正在尝试foreach在 Javascript 中实现 PHP 循环
我的多维数组的 PHP 代码是:

echo json_encode($jsonPoly);
foreach( $jsonPoly as $Route => $cordArray) { 
    echo $Route.': ';
    echo implode(',',$cordArray);    
}

我的多维数组输出是:

{"Route1":["22.2740,70.8275"],
"Route2":["22.2739,70.8276"],
"Route3":["22.2739,70.8276"],
"Route4":["22.2739,70.8276"],
"Route5":["22.2731,70.8303","22.2879,70.8264","22.3027,70.8125","22.3067,70.8137","22.2973,70.8158","22.2936,70.8201","22.2848,70.8157","22.2815,70.8161","22.2803,70.8245","22.2739,70.8276"],"Route6":["22.2739,70.8277"]}<br>

这是我尝试用 javascript 打印它

var jsonPoly=new Array();                               
jsonPoly = '"<?php echo json_encode($jsonPoly); ?>"'; 
for(var Route in jsonPoly){
    // printing each "Route"
}


我的 for 循环有问题吗?还是在我的多维数组中?
我想得到一些建议。
谢谢!

4

1 回答 1

1

要在 JavaScript 中获取值,您可以json_encode直接使用以下输出:

jsonPoly = <?php echo json_encode($jsonPoly); ?>;

这让你:

val = {"foo":"bar"};

与:

val = '"{"foo":"bar"}"'; // this would need a JSON.parse
于 2013-08-08T09:25:22.520 回答