如何使用php将值从函数传递到数组

如何使用php将值从函数传递到数组,php,arrays,foreach,Php,Arrays,Foreach,我已经阅读了关于这个主题的问答,但不幸的是它没有回答我的问题,因为我是PHP的初学者 我正在使用一个函数在谷歌地图上显示一个多边形。那很好用。坐标存储在以下变量中: $polygon = array( "43.231297 -79.813721", "43.238438 -79.810768", "43.230335 -79.809395", "43.230312 -79.809296", "43.240208 -79.808983", "43.230225 -79.808884", "43.2

我已经阅读了关于这个主题的问答,但不幸的是它没有回答我的问题,因为我是PHP的初学者

我正在使用一个函数在谷歌地图上显示一个多边形。那很好用。坐标存储在以下变量中:

$polygon = array(
"43.231297 -79.813721",
"43.238438 -79.810768",
"43.230335 -79.809395",
"43.230312 -79.809296",
"43.240208 -79.808983",
"43.230225 -79.808884",
"43.240116 -79.808617",
"43.229823 -79.807388",
"43.231235 -79.802649",
"43.237137 -79.800774",
"43.231297 -79.813721"
);
现在我想动态地从MySQL数据库中获取纬度和经度。下面的代码运行良好,并返回所需的坐标:

            <?
                foreach ($BusinessAreaMunich as $item) {
                echo "new google.maps.LatLng(" .$item['AreaCoordLatitude'] . "," .$item['AreaCoordLongitude'] . "), \n";
            }
            ?>
现在我知道这不管用,但我不知道如何解决我的问题。您能告诉我如何解决这个问题吗?

正确的代码是:

$polygon = array();  // define `$polygon` as array
foreach ($BusinessAreaMunich as $item) {
    // create `string` value as a result of concatenating coords and a space
    $coords = $item['AreaCoordLatitude'] . ' ' . $item['AreaCoordLongitude'];

    // append a `string` to `$polygon`
    $polygon[] = $coords;

    // or simply:
    // $polygon[] = $item['AreaCoordLatitude'] . ' ' . $item['AreaCoordLongitude'];
}

// output to see what you have
print_r($polygon);

请学习基本的php语法。非常好,谢谢:-)
$polygon = array();  // define `$polygon` as array
foreach ($BusinessAreaMunich as $item) {
    // create `string` value as a result of concatenating coords and a space
    $coords = $item['AreaCoordLatitude'] . ' ' . $item['AreaCoordLongitude'];

    // append a `string` to `$polygon`
    $polygon[] = $coords;

    // or simply:
    // $polygon[] = $item['AreaCoordLatitude'] . ' ' . $item['AreaCoordLongitude'];
}

// output to see what you have
print_r($polygon);