从文本到数组的坐标[preg_match]

从文本到数组的坐标[preg_match]

问题描述:

Maybe someone can help me get coordinates from text to array?

I'm trying use preg_match but no success.

I can remove geometry":{"paths":[[[**" and **]]}}]}) then use some times 'explode' and get coo, but I don't like this idea

Coordinates:

"geometry":{"paths":
[[[485749.91999999998,6108180.6500000004],
[485722.35999999999,6108206],
[485691.14000000001,6108234.3099999996],
[485400.84999999998,6108513.1500000004],
[485368.60999999999,6108545.46],
[485301.53999999998,6108613.1900000004],
[484054.82000000001,6109868.9100000001],
[484051.17566666665,6109872.6840000004]]]}}]});

也许有人可以帮助我获取从文本到数组的坐标? p>

I 尝试使用 preg_match code>但没有成功。 p>

我可以删除 geometry“:{”paths“:[[[**”和** ]]}}]}) code> 然后使用'爆炸'并获得咕咕声,但我不喜欢这个想法 p>

坐标: p>

  “几何”:{ “路径”:\ N  -  [[[485749.91999999998,6108180.6500000004] \ N [485722.35999999999,6108206],\ N [485691.14000000001,6108234.3099999996],\ N [485400.84999999998,6108513.1500000004]  ,\ N [485368.60999999999,6108545.46],\ N [485301.53999999998,6108613.1900000004],\ N [484054.82000000001,6109868.9100000001],\ N [484051.17566666665,6109872.6840000004]]]}}]}); 
 代码>  PRE>  
  div>

To figure out my approach in the comment above. Convert the string to valid JSON. Afterwards you can decode the string using json_decode():

$jsonArr = json_decode('{' . substr($string, 0, -4), true);
$coordinates= $jsonArr['geometry']['paths'][0]);

var_dump($coordinates):

So you end up with an array of coordinate pairs:

array(8) {
  [0]=>
  array(2) {
    [0]=>
    float(485749.92)
    [1]=>
    float(6108180.65)
  }
  [1]=>
  array(2) {
    [0]=>
    float(485722.36)
    [1]=>
    int(6108206)
  }
  [2]=>
  array(2) {
    [0]=>
    float(485691.14)
    [1]=>
    float(6108234.31)
  }
  [3]=>
  array(2) {
    [0]=>
    float(485400.85)
    [1]=>
    float(6108513.15)
  }
  [4]=>
  array(2) {
    [0]=>
    float(485368.61)
    [1]=>
    float(6108545.46)
  }
  [5]=>
  array(2) {
    [0]=>
    float(485301.54)
    [1]=>
    float(6108613.19)
  }
  [6]=>
  array(2) {
    [0]=>
    float(484054.82)
    [1]=>
    float(6109868.91)
  }
  [7]=>
  array(2) {
    [0]=>
    float(484051.175667)
    [1]=>
    float(6109872.684)
  }
}

This is invalid Javascript object notation. Running it through JSONLint shows that it has an invalid array length. You might want to provide more context for your question.

Assuming you have something similar to the above but with valid JSON, you can use the json_decode method to convert a valid JSON string into PHP arrays and objects.