怎么可能是将一个非常长的mysql线串转换为数组的最佳方法(逐点),我认为使用explode就是这样,但是在传递string参数之前需要进行大量文本操作才能爆炸。< / p>
这是查询结果的var_dump:
array (size=1)
'route' => string 'LINESTRING(-25.3233034167988 -57.640113830566406,-25.3229348965009 -57.64023184776306,-25.322653656571582 -57.63954520225525,-25.323410093517822 -57.63928771018982,-25.3229058027454 -57.6378607749939,-25.322973688164012 -57.63678789138794,-25.323371301994506 -57.63360142707825,-25.322003892857673 -57.633676528930664,-25.31995759927745 -57.633376121520996,-25.319336915717006 -57.63331174850464,-25.317988857651912 -57.63302206993103,-25.317377862364236 -57.63294696807861,-25.317688209562228 -57.62954592704773'... (length=4354)
目标是拥有一个这样的数组: 路线:
array (size=4354)
0 => { x=> '-25.3233034167988', y=> '-57.640113830566406'}
1 => { x=> '-25.3229348965009 ', y=> '-57.64023184776306'}
等等
使用nickb代码,结果是:
array (size=112)
0 =>
array (size=2)
'x' => string '-25.3233034167988' (length=17)
'y' => string '-57.640113830566406' (length=19)
1 =>
array (size=2)
'x' => string '-25.3229348965009' (length=17)
'y' => string '-57.64023184776306' (length=18)
2 =>
array (size=2)
'x' => string '-25.322653656571582' (length=19)
'y' => string '-57.63954520225525' (length=18)
完美
答案 0 :(得分:1)
我会做一些简单的字符串操作,然后使用str_getcsv()
形成数组:
$str = $array['route']; // Get the string from the array
// Get rid "LINESTRING(" and trailing ")"
$str = substr( $str, 11, (strlen( $str) - 1) - 11);
现在,您只需要输入x
和y
键,就像这样:
// Form the objective array:
$objective = array();
foreach( str_getcsv( $str) as $k => $v) {
list( $x, $y) = explode( ' ', $v);
$objective[$k]['x'] = $x;
$objective[$k]['y'] = $y;
}
您可以从this demo看到这将打印:
Array ( [0] => Array ( [x] => -25.3233034167988 [y] => -57.640113830566406 ) ... etc ...)
答案 1 :(得分:1)
我认为解析字符串更有效。
$route = 'LINESTRING(-25.3233034167988 -57.640113830566406,-25.3229348965009 -57.64023184776306,-25.322653656571582 -57.63954520225525)';
preg_match_all('/([0-9.\-]+) ([0-9.\-]+)/', $route, $matches);
$result = array_map(function($x, $y)
{
return ['x' => $x, 'y' => $y];
}, $matches[1], $matches[2]);
var_dump($result);
答案 2 :(得分:0)
我认为您可以使用两次爆炸功能来创建新阵列,然后循环遍历它并按预期分配键
$string = array('route' => 'LINESTRING(-25.3233034167988 -57.640113830566406,-25.3229348965009 -57.64023184776306,-25.322653656571582 -57.63954520225525,-25.323410093517822 -57.63928771018982,-25.3229058027454 -57.6378607749939,-25.322973688164012 -57.63678789138794,-25.323371301994506 -57.63360142707825,-25.322003892857673 -57.633676528930664,-25.31995759927745 -57.633376121520996,-25.319336915717006 -57.63331174850464,-25.317988857651912 -57.63302206993103,-25.317377862364236 -57.63294696807861,-25.317688209562228 -57.62954592704773');
$new_array = array();
$final_array = array();
foreach($string as $data)
{
$array = explode(',', $data);
foreach($array as $value)
{
$new_array []= explode(' ', str_replace('LINESTRING(', '', $value));
}
$array_key = array('x', 'y');
foreach($new_array as $val)
{
$final_array []= (array_combine($array_key, $val));
}
}
var_dump($final_array);
这将输出
array (size=13)
0 =>
array (size=2)
'x' => string '-25.3233034167988' (length=17)
'y' => string '-57.640113830566406' (length=19)
1 =>
array (size=2)
'x' => string '-25.3229348965009' (length=17)
'y' => string '-57.64023184776306' (length=18)
2 =>
array (size=2)
'x' => string '-25.322653656571582' (length=19)
'y' => string '-57.63954520225525' (length=18)
3 =>
array (size=2)
'x' => string '-25.323410093517822' (length=19)
'y' => string '-57.63928771018982' (length=18)
4 =>
array (size=2)
'x' => string '-25.3229058027454' (length=17)
'y' => string '-57.6378607749939' (length=17)
5 =>
array (size=2)
'x' => string '-25.322973688164012' (length=19)
'y' => string '-57.63678789138794' (length=18)
//and so on