douzhou7124 2014-06-05 04:40
浏览 12
已采纳

PHP查询结果中的引用数组值

I am trying to create a geoJSON array, but I am having difficulty access the values inside the query result. When I test my function by hardcoding the "coordinates" to the values '2' and '4' everything works fine.

However, when I then try to set the "coordinates" by referencing the $row['v2_lat'] and $row['v2_lng'], this causes an error. Here is the PHP code:

function getGeoJSON2(){
$m = $this->input->POST('municipality');
$l  = $this->input->POST('saleslimit'); 
$q = $this->db->query("SELECT RollNum, Address, v2_lat, v2_lng FROM tblontario WHERE Municipality = '".$m."'"." LIMIT ".$l);    
$res = $q->result();

if(!empty($res)){
    $geoArr = Array(
         "type" => "MultiPoint",
         "coordinates" => Array()
    );  
    foreach ($q->result() as $row) {
      //$geoArr["coordinates"][] = Array(2,4);
        $geoArr["coordinates"][] = Array($row['v2_lat'],$row['v2_lng']);        
    }
    $geoJSON = json_encode($geoArr);
    echo $geoJSON;
 } else {
     echo "{}";
 }
}   

And here is a sample of the query result and after applying json_encode:

[
{
    "RollNum": "193601001000100",
    "Address": "12 STEELES AVE E",
    "v2_lat": "43.8561002",
    "v2_lng": "-79.3370188"
},
{
    "RollNum": "193601001000400",
    "Address": "18 STEELES AVE E",
    "v2_lat": "43.7986849",
    "v2_lng": "-79.4178564"
},
{
    "RollNum": "193601001002100",
    "Address": "36 STEELES AVE E",
    "v2_lat": "43.7987492",
    "v2_lng": "-79.4169781"
}
]

I don't understand why I can't seem to be able to access the $row['v2_lat'] and $row['v2_lng'] values in my foreach loop. Any ideas? Thanks.

EDIT: I wanted to see the $res variable and this is the format of the array that comes back from the query:

Array
(
[0] => stdClass Object
    (
        [RollNum] => 180501000100100
        [Address] => 2 HILEY AVE
        [v2_lat] => 43.8526509
        [v2_lng] => -79.0499877
    )

[1] => stdClass Object
    (
        [RollNum] => 180501000100200
        [Address] => 4 HILEY AVE
        [v2_lat] => 43.8526678
        [v2_lng] => -79.0501957
    )

[2] => stdClass Object
    (
        [RollNum] => 180501000100300
        [Address] => 6 HILEY AVE
        [v2_lat] => 43.8526848
        [v2_lng] => -79.0504037
    )
)

What is the significance of the difference between these last two results?

  • 写回答

1条回答 默认 最新

  • dongzi1959 2014-06-05 07:53
    关注

    You access it like:

    $lat = $row->v2_lat; //and not like $row['v2_lat']
    

    Or if you want to access it like a simple array, you will need to change the following line:

    //$res = $q->result(); //this produces objects
    $res = $q->result_array();  //this produces array
    

    This will produce an array for you of the results and you can use your earlier code as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度