BarnabyAaron
BarnabyAaron

Reputation: 3

PHP I'm trying to split a string of Lat Lng Waypoints, whats the best method to use

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?

Upvotes: 0

Views: 162

Answers (1)

madfriend
madfriend

Reputation: 2430

$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));

Upvotes: 4

Related Questions