在PHP中从数组(Lat Long)获取数字

时间:2022-09-15 11:49:00

I am receiving an array with strings of Lat/Long in PHP from android like this:

我从android上接收到一个带有Lat/Long字符串的数组:

$array = array(
"parametros1" =>"lat/lng: (-33.36808,-70.74779)",
"parametros2" =>"lat/lng: (-33.36826,-70.74685)",
"parametros3" =>"lat/lng: (-33.36867,-70.745)",
"parametros4" =>"lat/lng: (-33.36875,-70.74462)",
"parametros5" =>"lat/lng: (-33.36879,-70.74436)",
"parametros6" =>"lat/lng: (-33.36882,-70.74415)",
"parametros7" =>"lat/lng: (-33.36888,-70.74387)",
"parametros8" =>"lat/lng: (-33.36905,-70.74364)",
"parametros9" =>"lat/lng: (-33.3691,-70.74347)",
"parametros10"=>"lat/lng: (-33.36948,-70.7417)" 
);

And I want to store the values of Lat/lng in 2 arrays, how can I get the values separately ?

我想将Lat/lng的值存储在2个数组中,如何分别得到这些值?

Ps:Sorry for my bad english,Thanks.-

Ps:对不起,我的英语不好,谢谢。

2 个解决方案

#1


2  

This is gonna get a little convoluted, and thats cause some data processing is needed.

这会有点复杂,这就需要一些数据处理。

$lat = array(); 
$lon = array(); 

foreach($array as $k => $v){
 $v = str_replace('lat/lng: (','',$v);
 $v = str_replace(')','',$v);
 $v = explode(',', $v);
 $lat[] = $v[0];
 $lon[] = $v[1];
}

print_r($lat);
print_r($lon);

#2


2  

Use list to map the results of explode on preg_replace -> $latLng to 2 arrays.

使用列表将preg_replace -> $latLng的爆炸结果映射到两个阵列。

foreach($array as $key => $latLng){
    list($arrayLat[],$arrayLng[]) = explode(",", preg_replace('/[^-\d.,]/', '', $latLng));
}

Ideone Demo 2

Ideone演示2

#1


2  

This is gonna get a little convoluted, and thats cause some data processing is needed.

这会有点复杂,这就需要一些数据处理。

$lat = array(); 
$lon = array(); 

foreach($array as $k => $v){
 $v = str_replace('lat/lng: (','',$v);
 $v = str_replace(')','',$v);
 $v = explode(',', $v);
 $lat[] = $v[0];
 $lon[] = $v[1];
}

print_r($lat);
print_r($lon);

#2


2  

Use list to map the results of explode on preg_replace -> $latLng to 2 arrays.

使用列表将preg_replace -> $latLng的爆炸结果映射到两个阵列。

foreach($array as $key => $latLng){
    list($arrayLat[],$arrayLng[]) = explode(",", preg_replace('/[^-\d.,]/', '', $latLng));
}

Ideone Demo 2

Ideone演示2