fork download
  1. <?php
  2.  
  3. $str = 'POLYGON((-22.886145457836463 -43.118764097835, -22.88643210096987 -43.118324215556555, -22.886694032959 -43.117846782351904, -22.886763222456636 -43.11767512097496, -22.886782990878046 -43.117605383540564, -22.886886775043237 -43.11763757004875, -22.887133879879073 -43.11891966595787, -22.88630360584699 -43.11942392125267, -22.886115805063966 -43.118823106433325, -22.886145457836463 -43.118764097835))';
  4.  
  5. function array_final($str)
  6. {
  7. $str = str_replace(['POLYGON','((','))'], [''], $str);
  8. $str = explode(" ", trim($str));
  9. $arrays = [];
  10. for($i = 0; $i < count($str); $i += 2)
  11. {
  12. $arrays[] = ['lat' => $str[$i], 'lng' => $str[$i+1]];
  13. }
  14. return $arrays;
  15. }
  16.  
  17. var_dump(array_final($str));
Success #stdin #stdout 0s 82880KB
stdin
Standard input is empty
stdout
array(10) {
  [0]=>
  array(2) {
    ["lat"]=>
    string(19) "-22.886145457836463"
    ["lng"]=>
    string(17) "-43.118764097835,"
  }
  [1]=>
  array(2) {
    ["lat"]=>
    string(18) "-22.88643210096987"
    ["lng"]=>
    string(20) "-43.118324215556555,"
  }
  [2]=>
  array(2) {
    ["lat"]=>
    string(16) "-22.886694032959"
    ["lng"]=>
    string(20) "-43.117846782351904,"
  }
  [3]=>
  array(2) {
    ["lat"]=>
    string(19) "-22.886763222456636"
    ["lng"]=>
    string(19) "-43.11767512097496,"
  }
  [4]=>
  array(2) {
    ["lat"]=>
    string(19) "-22.886782990878046"
    ["lng"]=>
    string(20) "-43.117605383540564,"
  }
  [5]=>
  array(2) {
    ["lat"]=>
    string(19) "-22.886886775043237"
    ["lng"]=>
    string(19) "-43.11763757004875,"
  }
  [6]=>
  array(2) {
    ["lat"]=>
    string(19) "-22.887133879879073"
    ["lng"]=>
    string(19) "-43.11891966595787,"
  }
  [7]=>
  array(2) {
    ["lat"]=>
    string(18) "-22.88630360584699"
    ["lng"]=>
    string(19) "-43.11942392125267,"
  }
  [8]=>
  array(2) {
    ["lat"]=>
    string(19) "-22.886115805063966"
    ["lng"]=>
    string(20) "-43.118823106433325,"
  }
  [9]=>
  array(2) {
    ["lat"]=>
    string(19) "-22.886145457836463"
    ["lng"]=>
    string(16) "-43.118764097835"
  }
}