0

I am using the google maps api to return a string of waypoint Lat/Lng values. and example of this is:

(51.4409138, -2.542236799999955),(51.44766809999999, -2.544380100000012),(51.4355129, -2.5526174999999967)

My target is to result in something like this:

array (
   0 => array (0 => 51.4409138,1 => -2.542236799999955),
   1 => array (0 => 51.44766809999999,1 => -2.544380100000012),
   2 => array (0 => 51.4355129,1 => -2.5526174999999967)
)

What can you suggest is the best method to use?

0

1 Answer 1

4
$first = explode('),(', $coords);
foreach ($first as $result) {
    $second[] = explode(', ', trim($result, '()`');
}

$second is what you need.

For your interest, here is a one-liner (5.3):

$desired_output = array_map(function($pair) {
                               return explode(', ', trim($pair, '()`');
                            }, explode('),(', $original_string));
2
  • Hmm i thought they might of been a easier method to use like preg_split with a certain expression but I guess explode works just fine. Jul 20, 2012 at 13:23
  • Yes, perhaps. But this one is more straightforward.
    – madfriend
    Jul 20, 2012 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.