Skip to content
Advertisement

Arrange values in array php / mysql

I am exporting data from mysqli to google firebase DB. In firebase table one of the column named siteGeoCode (latitude and longitude) having values like [17.426083,78.439241] in array format. But in mysqli table two separate columns are there for latitude and longitude. I am trying to fetch records like below and converting those to array format.

$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);
}

// output

[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

Advertisement

Answer

If you are wanting to place the result of the latitude/longitude columns into the result grouped, do the manipulations to the row, then add it to your result set, abit like below.

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

    // place in result
    $result[] = $row;
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement