【问题标题】:Arrange values in array php / mysql在数组 php / mysql 中排列值
【发布时间】:2019-09-28 12:03:45
【问题描述】:

我正在将数据从 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] => 
    )

)

但我想要的输出应该如下所示

[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】:

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

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

    【讨论】:

      【解决方案2】:

      您可以使用以下代码。

      $output_array = array();
      while($sel_row = mysqli_fetch_assoc($res)) {
      
      
      $output_array[] = array(
                'companyId' => $sel_row['companyId'],
                'countryCode' => $sel_row['countryCode'],
                'createdBy' => $sel_row['createdBy'],
                'createdDate' => $sel_row['createdDate'],
                'siteGeoPoint' => array($sel_row['latitude'], $sel_row['longitude']),
                'siteName' => $sel_row['siteName'],
                'status' => $sel_row['status'],
              );
          }
      print_r($output_array);
      

      你可以看到你使用数组和我所做的不同。从你的输出我猜你已经打印了不同的数组。

      希望代码能得到你想要的结果!

      【讨论】:

      • 我只有一行值。我在那个表中有 3 行
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多