如何在PHP中访问多维数组?

时间:2019-03-13 09:08:44

标签: php arrays

您好,我正在使用mysqli_fetch_assoc()从数据库中获取数据并获得此结果

Array
(
    [poi_id] => 1
    [created_id] => 1
    [tour_id] => 1
    [poi_title] => Kankariya Lake
    [poi_description] => Kankariya lake is a kankariya lake.
    [poi_category] => Historical
    [poi_base_price] => 10
    [poi_type] => driving
    [poi_address] => Kankaariya lake near maninagar
    [poi_city] => Ahmedabad
    [poi_state] => Gujaray
    [poi_country] => India
    [poi_latitude] => 23.006353
    [poi_longitude] => 72.601181
    [created_by] => Admin
    [status] => 1
    [time] => 2019-03-06 15:14:25
    [distance] => 7.981589182296636
)
Array
(
    [poi_id] => 2
    [created_id] => 1
    [tour_id] => 2
    [poi_title] => Kankariya Lake
    [poi_description] => Kankariya lake is a kankariya lake.
    [poi_category] => Historical
    [poi_base_price] => 10
    [poi_type] => driving
    [poi_address] => Kankaariya lake near maninagar
    [poi_city] => Ahmedabad
    [poi_state] => Gujaray
    [poi_country] => India
    [poi_latitude] => 23.006353
    [poi_longitude] => 72.601181
    [created_by] => Admin
    [status] => 1
    [time] => 2019-03-07 16:51:11
    [distance] => 7.981589182296636
)
Array
(
    [poi_id] => 3
    [created_id] => 1
    [tour_id] => 2
    [poi_title] => Kankariya Lake
    [poi_description] => Kankariya lake is a kankariya lake.
    [poi_category] => Historical
    [poi_base_price] => 10
    [poi_type] => driving
    [poi_address] => Kankaariya lake near maninagar
    [poi_city] => Ahmedabad
    [poi_state] => Gujaray
    [poi_country] => India
    [poi_latitude] => 23.006353
    [poi_longitude] => 72.601181
    [created_by] => Admin
    [status] => 1
    [time] => 2019-03-07 17:06:44
    [distance] => 7.981589182296636
)

所以现在我想在for循环中访问垂直数组元素,以便如何访问该元素 我想将纬度和经度与数组的所有其他纬度进行比较 我更新的代码

while ($poi_array = mysqli_fetch_assoc($qry_res)) 
{
    $poi_id=$poi_array['poi_id'];
    $new[]=$poi_array;
    //print_r($new);
    $lat1=$poi_array['poi_latitude'];
    $lon1=$poi_array['poi_longitude'];  
    //echo $new['poi_longitude'][1];

    $theta = $lon1 - $lon2;
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    $km= $miles * 1.609344;
    $meter=$km*1000;
    //echo round($meter);
    if($meter>1000)
    {                   
        $response2['success'] = true;
        $response2['message'] = "Pointof Interest list successfully fetched";
    }
    $fetch_media="SELECT * FROM poito_poi_media WHERE poi_id='".$poi_id."'";
    $media_qry_res = mysqli_query($con,$fetch_media);
    if (mysqli_num_rows($media_qry_res) > 0) 
    {
        while ($media_array = mysqli_fetch_assoc($media_qry_res)) 
        {
            $response4['media_id'] = $media_array['media_id'];
            $response4['image'] = $media_array['poi_image'];
            $response4['video'] = $media_array['poi_video'];
            $response4['ar'] = $media_array['poi_ar'];
            $response5[]=$response4;
            $response6['media_array']=$response5;
        }
    }
    else
    {
        $response6['media_array']=null;
    }
    $response['poi_id'] = $poi_array['poi_id'];
    $response['poi_title'] = $poi_array['poi_title'];
    $response['poi_description'] = $poi_array['poi_description'];
    $response['poi_category'] = $poi_array['poi_category'];
    $response['poi_base_price'] = $poi_array['poi_base_price'];
    $response['poi_type'] = $poi_array['poi_type'];
    $response['poi_address'] = $poi_array['poi_address'];
    $response['poi_city'] = $poi_array['poi_city'];
    $response['poi_state'] = $poi_array['poi_state'];
    $response['poi_country'] = $poi_array['poi_country'];
    $response['poi_latitude'] = $poi_array['poi_latitude'];
    $response['poi_longitude'] = $poi_array['poi_longitude'];
    unset($response5);
    $response['media_array'] = $response6['media_array'];

    $response3[]=$response;
    $response2['data']=$response3;
}

如果数组中其他元素的距离在1000米之内,则仅给出一个元素的响应,而无需在数组中添加其他数据

这意味着如果一个地方多次出现,那么这也是placeinfo也会删除同一地方,那么我们只需要显示一个地方即可

1 个答案:

答案 0 :(得分:0)

尝试一下:

while ($row = mysql_fetch_assoc($result)) {
    $rows[] = $row;
}

var_dump($rows);
//You can remove this
//it's just so that you can see the contents of the $row array

然后,您将可以遍历$rows数组以查找所需的任何值。您可以像这样将$rows放入foreach()循环中:

foreach($rows as $row){
    //$row['poi_latitude']
    //$row['poi_longitude']
}