0

我有一个 php 数组,我需要将其转换并分配给var locationsArray

所需格式:

var locationsArray = [
    ['Google Official','1600 Amphitheatre Parkway, Mountain View, USA'],
    ['Google 1','112 S. Main St., Ann Arbor, USA'], 
    ['Google 2','10 10th Street NE, Suite 600 USA']
];
4

4 回答 4

2
    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-12T18:57:22.267 回答
0

试试喜欢

$NewJsonData = json_encode(locationsArray);
echo $NewJsonData;  //Alert it in javascript   

试试这个链接

您可以将变量 $NewJsonData 分配给您的 js 变量

于 2013-06-11T06:16:42.177 回答
0

这将是这样的:

<script type="text/javascript">
    var locationsArray = <?php echo json_encode(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5)); ?>
</script>

不知道 php 语法,对不起 :) ref

于 2013-06-11T06:17:53.513 回答
0

这将对您有所帮助。

<script type='text/javascript'>
<?php
$php_array = array('abc','def','ghi');
$js_array = json_encode($php_array);
echo "var javascript_array = ". $js_array . ";\n";
?>
</script>
于 2013-06-11T06:19:00.857 回答