Separate Coordinate String in Array with separate Lat Long?

Asked

Viewed 69 times

0

I need to separate a coordinate string lat/long in PHP which are in the format below in an array separating latitude from longitude:

[-23.61025,-46.5871],[-23.61006,-46.58824],[-23.61005,-46.58831],[-23.60999,-46.5886],[-23.60988,-46.58906],[-23.60969,-46.58946],[-23.60961,-46.5896],[-23.60944,-46.58979],[-23.60917,-46.59014]

I tried with Regex, but I did not get the expected result.

I need to make sure they’re on a record of array separate, to perform separate reading and comparison of each of these lat/long.

  • so it’s a text all this?

  • 1

    Yeah, it’s a string.

2 answers

0


Try the following:

<?php
//
$re = '/\[.*?\]/m';
$str = '[-23.61025,-46.5871],[-23.61006,-46.58824],[-23.61005,-46.58831],[-23.60999,-46.5886],[-23.60988,-46.58906],[-23.60969,-46.58946],[-23.60961,-46.5896],[-23.60944,-46.58979],[-23.60917,-46.59014]';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

$_arr = [];

foreach ($matches as $value){

    $arr = explode(",",$value[0]);

    $lat = str_replace("[","", $arr[0]);

    $long = str_replace("]","", $arr[1]);

    array_push($_arr, ["lat" => $lat, "long" => $long]);
}

var_dump($_arr);

?>

Exit:

array(9) {
  [0]=>
  array(2) {
    ["lat"]=>
    string(9) "-23.61025"
    ["long"]=>
    string(8) "-46.5871"
  }
  [1]=>
  array(2) {
    ["lat"]=>
    string(9) "-23.61006"
    ["long"]=>
    string(9) "-46.58824"
  }
  [2]=>
  array(2) {
    ["lat"]=>
    string(9) "-23.61005"
    ["long"]=>
    string(9) "-46.58831"
  }
  [3]=>
  array(2) {
    ["lat"]=>
    string(9) "-23.60999"
    ["long"]=>
    string(8) "-46.5886"
  }
  [4]=>
  array(2) {
    ["lat"]=>
    string(9) "-23.60988"
    ["long"]=>
    string(9) "-46.58906"
  }
  [5]=>
  array(2) {
    ["lat"]=>
    string(9) "-23.60969"
    ["long"]=>
    string(9) "-46.58946"
  }
  [6]=>
  array(2) {
    ["lat"]=>
    string(9) "-23.60961"
    ["long"]=>
    string(8) "-46.5896"
  }
  [7]=>
  array(2) {
    ["lat"]=>
    string(9) "-23.60944"
    ["long"]=>
    string(9) "-46.58979"
  }
  [8]=>
  array(2) {
    ["lat"]=>
    string(9) "-23.60917"
    ["long"]=>
    string(9) "-46.59014"
  }
}

Demonstration: https://paiza.io/projects/ctetBoD2GpgFT8zEZ4n2Kg?language=php

REFERENCE:

  1. https://www.php.net/manual/en/function.str-replace.php
  2. https://regex101.com
  • Perfect, exactly what I needed. Thank you very much!

0

I had the idea the following way, break this text at the point ],[ where I can verify the division of these coordinates and array_map assemble the coordinates into a new array associative as follows:

Code:

$texto = '[-23.61025,-46.5871],[-23.61006,-46.58824],[-23.61005,-46.58831],
          [-23.60999,-46.5886],[-23.60988,-46.58906],[-23.60969,-46.58946],
          [-23.60961,-46.5896],[-23.60944,-46.58979],[-23.60917,-46.59014]';

function get_coordinates($texto) {
    $parte = explode('],[', $texto);
    return array_map(
        function($item) { 
            list($lat, $long) = explode(',', substr(str_replace(']','',$item));
            return ['lat' => $lat, 'long' => $long];
        }, $parte
    );
}

print_r(get_coordinates($texto));

Exit:

Array
(
    [0] => Array
        (
            [lat] => -23.61025
            [long] => -46.5871
        )

    [1] => Array
        (
            [lat] => 23.61006
            [long] => -46.58824
        )

    [2] => Array
        (
            [lat] => 23.61005
            [long] => -46.58831
        )

    [3] => Array
        (
            [lat] => 23.60999
            [long] => -46.5886
        )

    [4] => Array
        (
            [lat] => 23.60988
            [long] => -46.58906
        )

    [5] => Array
        (
            [lat] => 23.60969
            [long] => -46.58946
        )

    [6] => Array
        (
            [lat] => 23.60961
            [long] => -46.5896
        )

    [7] => Array
        (
            [lat] => 23.60944
            [long] => -46.58979
        )

    [8] => Array
        (
            [lat] => 23.60917
            [long] => -46.59014
        )

)

With this data in array you can access the positions and view the information as you need

Browser other questions tagged

You are not signed in. Login or sign up in order to post.