-1

I'm get data string lat and long google maps polygon. I want to convert this value to array .

$value = "(-6.2811957386588855, 106.70141951079609),(-6.281142416506361, 106.70432702536823),(-6.2781776962328815, 106.70438066954853),(-6.2781776962328815, 106.70136586661579)";

I want the result like this Array :

$polygon = array(
        array(-6.2811957386588855, 106.70141951079609),
        array(-6.281142416506361, 106.70432702536823),
        array(-6.2781776962328815, 106.70438066954853),
        array(-6.2781776962328815, 106.70136586661579),
    );
Lets-c-codeigniter
  • 682
  • 2
  • 5
  • 18
Zaboy
  • 47
  • 1
  • 6
  • 2
    What is `$value` here? is that a string or array? – MH2K9 Sep 10 '19 at 07:33
  • 1
    where are you getting this data from initially in this format? – Professor Abronsius Sep 10 '19 at 07:34
  • Please be a bit more specific when asking a question: *What have you tried so far with a code example?* / *What do you expect?* / *What error do you get?* **For Help take a look at "[How to ask](//stackoverflow.com/help/how-to-ask)"** – liakoyras Sep 10 '19 at 07:37
  • Possible duplicate of [How to convert a comma separated string to an array?](https://stackoverflow.com/questions/2858121/how-to-convert-a-comma-separated-string-to-an-array) – LF00 Sep 10 '19 at 07:51

5 Answers5

3

You can convert the string to valid JSON by converting parentheses to square brackets and adding a [] layer around the outside, and then json_decode it:

$value = '(-6.2811957386588855, 106.70141951079609),(-6.281142416506361, 106.70432702536823),(-6.2781776962328815, 106.70438066954853),(-6.2781776962328815, 106.70136586661579)';
$polygon = json_decode('[' . str_replace(array('(', ')'), array('[', ']'), $value) . ']', true);
print_r($polygon);

Output:

Array
(
    [0] => Array
        (
            [0] => -6.2811957386589
            [1] => 106.7014195108
        )    
    [1] => Array
        (
            [0] => -6.2811424165064
            [1] => 106.70432702537
        )    
    [2] => Array
        (
            [0] => -6.2781776962329
            [1] => 106.70438066955
        )    
    [3] => Array
        (
            [0] => -6.2781776962329
            [1] => 106.70136586662
        )    
)

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95
1

You can use explode with trim and array_map

$r = explode('),(',trim($value,'()'));
$c = array_map(function($v){return explode(',',$v);}, $r);
print_r($c);

Working example : https://3v4l.org/fFAS0

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
1

You can use explode() function like this

$string = '(-6.2811957386588855, 106.70141951079609),(-6.281142416506361, 106.70432702536823),(-6.2781776962328815, 106.70438066954853),(-6.2781776962328815, 106.70136586661579)';
foreach(explode('),(',trim($string,'()')) as $single_array)
{
  $sub_array= array();
  foreach(explode(',',$single_array) as $sbs_array)
  {
      $sub_array[] = $sbs_array;
  }
  $result[] = $sub_array;
} 
print_r ($result);

Output :

Array
(
   [0] => Array
    (
        [0] => -6.2811957386588855
        [1] =>  106.70141951079609
    )

   [1] => Array
    (
        [0] => -6.281142416506361
        [1] =>  106.70432702536823
    )

   [2] => Array
    (
        [0] => -6.2781776962328815
        [1] =>  106.70438066954853
    )

   [3] => Array
    (
        [0] => -6.2781776962328815
        [1] =>  106.70136586661579
    ) 

)

Demo : https://3v4l.org/6HEAG

Lets-c-codeigniter
  • 682
  • 2
  • 5
  • 18
1

Using preg_match_all() and array_walk() you can parse the coordinates as an array

$value = '(-6.2811957386588855, 106.70141951079609),(-6.281142416506361, 106.70432702536823),(-6.2781776962328815, 106.70438066954853),(-6.2781776962328815, 106.70136586661579)';
preg_match_all('/\(([0-9\-\s,.]+)\)/', $value, $matches);
array_walk($matches[1], function(&$val) { $val = explode(',', $val); });
$coordinates = $matches[1];
print_r($coordinates);
  • Using preg_match_all() get all the coordinates as array of string
  • Using array_walk() make an iteration over the coordinated array and explode by the delimiter of comma (,)

MH2K9
  • 11,951
  • 7
  • 32
  • 49
0

I suggest using preg_match_all with array_map

$value = '(-6.2811957386588855, 106.70141951079609),(-6.281142416506361, 106.70432702536823),(-6.2781776962328815, 106.70438066954853),(-6.2781776962328815, 106.70136586661579)';


preg_match_all('#\(([\-\d\.]+),\s+([\-\d\.]+)\)#', $value, $matches);

$geo = array_map(function ($a, $b) {
   return [(float)$a, (float)$b];
}, $matches[1], $matches[2]);

Output:

array(4) {

[0]=>
  array(2) {
    [0]=> float(-6.2811957386589)
    [1]=> float(106.7014195108)
  }
  [1]=>
  array(2) {
    [0]=> float(-6.2811424165064)
    [1]=> float(106.70432702537)
  }
  [2]=>
  array(2) {
    [0]=> float(-6.2781776962329)
    [1]=> float(106.70438066955)
  }
  [3]=>
  array(2) {
    [0]=> float(-6.2781776962329)
    [1]=> float(106.70136586662)
  }
}

Regex

Robert
  • 19,800
  • 5
  • 55
  • 85
  • Using regex for it is like using machine learning for a hello world. @Rakesh Jakhar answer is cleaner and fastest. – Elias Soares Sep 10 '19 at 08:31
  • This analogy makes no sense. Regex is meant to extract data from strings, and this is a perfect example of using it. Invoking multiple functions doesn't make it clear at all. If the other developer saw that code, you would need to explain why you wrote like this. With REGEX it's straightforward. I can only agree that his answer might be faster. I haven't checked it though. – Robert Sep 10 '19 at 09:23