Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中从PHP数组填充Google地图时出现的问题_Javascript_Php_Google Maps_Google Visualization - Fatal编程技术网

在Javascript中从PHP数组填充Google地图时出现的问题

在Javascript中从PHP数组填充Google地图时出现的问题,javascript,php,google-maps,google-visualization,Javascript,Php,Google Maps,Google Visualization,我有一个PHP国家名称数组- <?php $countries_array = array("Russia", "Australia", "Chile"); ?> 我需要通过Javascript在地图上显示这些国家,并使用以下代码- function drawRegionsMap() { var data = new google.visualization.DataTable(); data.addColumn('string', 'Country'); <

我有一个PHP国家名称数组-

<?php 
$countries_array = array("Russia", "Australia", "Chile");
?>

我需要通过Javascript在地图上显示这些国家,并使用以下代码-

function drawRegionsMap() {
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Country');

<?php 
    foreach ($countries_array as $cty) {
        echo "data.addRow(['Country', " . $cty . "]);";
    }
?>

    var options = {backgroundColor: '#E7F2F4'};

    var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));

    chart.draw(data, options);
    }
函数drawRegionsMap(){
var data=new google.visualization.DataTable();
data.addColumn('string','Country');

注意PHP服务于
$countries\u array
,因此您需要在PHP脚本标记中输出数组。使用
addRow()
添加每个数据可能更容易,这使得语法更简单

function drawRegionsMap() {
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Country');

<?php 
    foreach ($countries_array as $cty) {
        print "data.addRow(['" . $cty . "']);" . PHP_EOL;
    }
?>

  var options = {backgroundColor: '#cccccc'};
  var chart = new   google.visualization.GeoChart(document.getElementById('regions_div'));

  chart.draw(data, options);
}
函数drawRegionsMap(){
var data=new google.visualization.DataTable();
data.addColumn('string','Country');

仍然不起作用。地图现在没有显示。用我的代码@SuspesticstHanks更新了我的问题。您的答案只需稍加修改即可。通过从-print“data.addRow(['Country',''.$cty.']];”)中删除'Country',即可;