0

我有一个数组 { $address = array(..........);} 我已将其转换为 {

foreach ($address as $key => $val){
    $points[] = "['{$val}', '{$val}']";
}
$output = join ("," , $points);
$req_format = strip_tags($output);

}

输出:['30 South Wacker Drive Floor 22 Chicago IL 60606', '30 South Wacker Drive Floor 22 Chicago IL 60606'],['288 Bishopsgate London, EC2M 4QP United Kingdom', '288 Bishopsgate London, EC2M 4QP United Kingdom '],['麦迪逊大道 260 号 8 楼纽约 NY 10016 ', '麦迪逊大道 260 号 8 楼纽约 NY 10016 ']

我需要将此值分配给 js 变量:

var locationsArray = [
['30 South Wacker Drive Floor 22 Chicago IL 60606', '30 South Wacker Drive Floor 22 Chicago IL 60606'],
['30 South Wacker Drive Floor 22 Chicago IL 60606', '30 South Wacker Drive Floor 22 Chicago IL 60606']
];

如何分配 php 变量 $req_format 等于 js 变量 locationArray = [??????];

4

2 回答 2

1

你可以使用 json 代替,比如

foreach ($address as $key => $val){
    $points[] = "['{$val}', '{$val}']";
}
$jsoned = json_encode($points);
//pass it to your js
var js_data = "<?php echo $jsoned; ?>";
于 2013-06-11T05:18:53.540 回答
0
foreach ($address as $key => $val){
    $val = strip_tags($val);
    $points[] = array($val, $val);
}
$json = json_encode($points);
$json = str_replace('\n',' ',$json);
$json = str_replace('\r',' ',$json);

var locations = '<?php echo $json;?>';
var locations_array = JSON.parse(locations);

var locationsArray = locations_array;
于 2013-06-12T19:02:23.577 回答