在数组php / mysql中排列值

问题描述 投票:0回答:1

我正在将数据从mysqli导出到Google Firebase DB。在Firebase表中,名为siteGeoCode(纬度和经度)的列之一具有数组格式的值,例如[17.426083,78.439241]。但是在mysqli表中,有两个单独的列用于纬度和经度。我正在尝试获取以下记录,并将其转换为数组格式。

$emp_array = array();
$sel_query = "select companyId,countryCode,createdBy,createdDat,`latitude`,`longitude`,siteName,`status` from customers limit 0,3";
$res = mysqli_query($mysqliConn,$sel_query);
while($sel_row = mysqli_fetch_assoc($res))
{
    $emp_array[]  = $sel_row;
    $lat_array['siteGeoCode'][0] = $sel_row['latitude'];
    $lat_array['siteGeoCode'][1] = $sel_row['longitude'];
    array_push($emp_array,$lat_array);
}

//输出

[0] => Array
(
[companyId] => iArwfGLC1lE6x3lO24cb
[countryCode] => 91
[createdBy] => X54P6gVJ7dA4Fi2fjEmc
[createdDate] => 2019-08-20 00:58:08
[latitude] => 15.6070347
[longitude] => 79.6146273
[siteName] => Ongole
[status] => 1
)

[1] => Array
(
[siteGeoCode] => Array
    (
        [0] => 15.6070347
        [1] => 79.6146273
    )

)

[2] => Array
(
[companyId] => YPbhSLWQfAR6ThhszSAf
[countryCode] => 91
[createdBy] => iArwfGLC1lE6x3lO24cb
[createdDate] => 2019-09-10 22:37:08
[latitude] => 
[longitude] => 
[siteName] => Madhap
[status] => 
)

[3] => Array
(
[siteGeoCode] => Array
    (
        [0] => 
        [1] => 
    )

)

// but my desired output should be like below

[0] => Array
(
[companyId] => iArwfGLC1lE6x3lO24cb
[countryCode] => 91
[createdBy] => X54P6gVJ7dA4Fi2fjEmc
[createdDate] => 2019-08-20 00:58:08
[siteGeoPoint] => Array
    (
        [0] => 15.6070347
        [1] => 79.6146273
    )
[siteName] => Ongole
[status] => 1
)

[1] => Array
(
[companyId] => YPbhSLWQfAR6ThhszSAf
[countryCode] => 91
[createdBy] => iArwfGLC1lE6x3lO24cb
[createdDate] => 2019-09-10 22:37:08
[siteGeoPoint] => Array
    (
        [0] => 
        [1] => 
    )
[siteName] => Madhap
[status] => 
)

How can I achieve this ? Any help would be greatly appreciated. Thanks in advance
php arrays array-merge
1个回答
0
投票

如果要将纬度/经度列的结果放入分组的结果中,请对该行进行操作,然后将其添加到结果集中,如下所示。

$result = [];
while ($row = mysqli_fetch_assoc($res)) {
    $row['siteGeoCode'] = [
        $row['latitude'],
        $row['longitude']
    ];
    unset($row['latitude']);
    unset($row['longitude']);

    $result[] = $row;
}
© www.soinside.com 2019 - 2024. All rights reserved.