如何使用PHP将多个数组合并为单个数组

如何使用PHP将多个数组合并为单个数组,php,arrays,Php,Arrays,我从数据库中查询地址列,并将多个地址传递给Google Geocoder API,然后将纬度和经度结果存储在$coordinate变量中。当ivar\u导出($coordinate)时我以以下格式获得输出: '[19.0759837,72.8776559]''[47.6062095,-122.3320708]''[12.9715987,77.5945627]' 但在使用print_r(数组($variablename))时我以以下格式获取输出: Array ( [0] => [19.0

我从数据库中查询地址列,并将多个地址传递给Google Geocoder API,然后将纬度和经度结果存储在
$coordinate
变量中。当i
var\u导出($coordinate)时我以以下格式获得输出:

'[19.0759837,72.8776559]''[47.6062095,-122.3320708]''[12.9715987,77.5945627]'
但在使用
print_r(数组($variablename))时我以以下格式获取输出:

Array
(
 [0] => [19.0759837,72.8776559]
)
Array
(
  [0] => [47.6062095,-122.3320708]
)
Array
(
  [0] => [12.9715987,77.5945627]
)
但我希望输出是在单个数组中,类似这样

Array
(
 [0] => [19.0759837,72.8776559],
 [1] => [47.6062095,-122.3320708],
 [2] => [12.9715987,77.5945627]
)
这是php代码:

<?php 
    // database connection code and query to fetch address column the database and pass address to geocode()
header("content-type: application/json");
        header('Access-Control-Allow-Origin: *');
        $config = parse_ini_file('./config.ini');
        $connect = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']);
        if (mysqli_connect_errno())
                {
                  echo "Failed to connect to MySQL: " . mysqli_connect_error();
                }
                $ordertracker = mysqli_query($connect, "select address from ordertracker");

                if (!$ordertracker)
                {
                    echo "Error fetching results: " . mysqli_error();

                }

        $address=array();
        while($row = mysqli_fetch_assoc($ordertracker)) {
             $address= $row['address'];
              geocode($address);  
        }
    function geocode($address)
     {
                $address = urlencode($address);
                $url = "http://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=false";
                $geocode=file_get_contents($url);
                $output= json_decode($geocode);
                $lat = $output->results[0]->geometry->location->lat;
                $long = $output->results[0]->geometry->location->lng;
                $coordinate = "[" . $lat . "," . $long . "]";  
                var_export($coordinate);  
      }
    ?>

如果您想要一个数组,那么不应该使用字符串文本和连接来构建它,因为这将导致一个字符串

相反,让函数生成一个包含两个值(纬度和经度)的数组,并将这些对附加到主循环中的主数组中:

$address = [];
while($row = mysqli_fetch_assoc($ordertracker)) {
    $address[] = geocode($row['address']);
}
print_r($address);

function geocode($address) {
    $address = urlencode($address);
    $url = "http://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=false";
    $geocode = file_get_contents($url);
    $output= json_decode($geocode);
    $lat = $output->results[0]->geometry->location->lat;
    $long = $output->results[0]->geometry->location->lng;
    $coordinate = [$lat, $long];  
    return $coordinate;
}

我很困惑。
$coordinate
是同时保持一个坐标还是三个坐标?如果有,你怎么取下一个?在一个循环中?$坐标变量保存所有三个变量,并且它将动态增长。
print\r(数组(您的字符串))
给出了
数组([0]=>[19.0759837,72.8776559][47.6062095,-122.3320708][12.9715987,77.5945627])
@Gauravkb那么,它是字符串吗?你能用
var\u export
而不是
echo
print\u r
重写问题中的示例吗?@Siguza当我使用var\u export($coordinate);我得到如下输出:
“[19.0759837,72.8776559]”[47.6062095,-122.3320708]”[12.9715987,77.5945627]”