duanpo1498 2017-08-23 11:55
浏览 27

如何在不更新数组的情况下向数组添加值

I have two rows (longitude and latitude) in my MySQL table. I want to get the two rows into a 2-dimensional array such that when the values (longitude and latitude) change in the database the values will be added to the array. Not that the values will be updated.

 while($mRow = $resultMarker->fetch_assoc()){
      $myArrayForMarkers [] = $mRow['latitude'];
      $myArrayForMarkers [] = $mRow['longitude'];       
  }

  print_r($myArrayForMarkers);

The issue I have in the code above is that instead of the new values to be added from the database to the array, the values in the array are being updated. Any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • duanfu7840 2017-08-23 12:38
    关注

    As I said in the comments, the wording of your question makes little sens. For example, I don't undurstand what you mean by 'Not that the values will be updated.' nor how your array being 'updated' is different to 'the values will be added to the array'.

    Please clarify your question, especially if my answer doesn't solve it so that those who will eventually come here while looking after a solution can understand if your issue is the same than theirs.

    I get that you try to craft a 2-dimension array. The code you provided produces 1-dimension arrays.

    If you want to store your datas in a 2-dimension array, I only see two solutions :

    Either your main array has two subarrays, each containing one type of data :

    while ($mRow = $resultMarker->fetch_assoc()) {
        $myArrayForMarkers['latitude'][] = $mRow['latitude'];
        $myArrayForMarkers['longitude'][] = $mRow['longitude'];
    }
    

    or en each entry of your array itself contains both information :

    while ($mRow = $resultMarker->fetch_assoc()) {
        $myArrayForMarkers[] = array('latitude' => $mRow['latitude'], 'longitude' => $mRow['longitude']);
    }
    

    That's the most I can do right now, since I don't understand the rest of your question.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值