简体   繁体   中英

Split and parse a string to create an array of associative arrays

How can I convert the following string into two dimensional array?

$coordinates = "9.499819 123.920318,9.490845 123.916563,9.484644 123.922292,9.49148 123.931519,9.499755 123.925683,9.499819 123.920318";

I want to separate pairs of values using the commas, then separate each pair by their delimiting space to form associative rows containing float-type values.

Desired Output:

$polygon = array(
    array('lat' => 9.499819, 'lng' => 123.920318),
    array('lat' => 9.490845, 'lng' => 123.916563),
    array('lat' => 9.484644, 'lng' => 123.922292),
    array('lat' => 9.49148, 'lng' => 123.931519),
    array('lat' => 9.499755, 'lng' => 123.925683),
    array('lat' => 9.499819, 'lng' => 123.920318)
);

By making iterated calls of sscanf() , you can dictate the data type of the parsed values and push the values into the result array.

Code: ( Demo )

$coordinates = "9.499819 123.920318,9.490845 123.916563,9.484644 123.922292,9.49148 123.931519,9.499755 123.925683,9.499819 123.920318";

foreach (explode(',', $coordinates) as $i => $latLng) {
    sscanf($latLng, '%f %f', $result[$i]['lat'], $result[$i]['lng']);
};
var_export($result);

Output:

array (
  0 => 
  array (
    'lat' => 9.499819,
    'lng' => 123.920318,
  ),
  1 => 
  array (
    'lat' => 9.490845,
    'lng' => 123.916563,
  ),
  2 => 
  array (
    'lat' => 9.484644,
    'lng' => 123.922292,
  ),
  3 => 
  array (
    'lat' => 9.49148,
    'lng' => 123.931519,
  ),
  4 => 
  array (
    'lat' => 9.499755,
    'lng' => 123.925683,
  ),
  5 => 
  array (
    'lat' => 9.499819,
    'lng' => 123.920318,
  ),
)

For a new contributor, a solution with foreach is easier to understand in my opinion. The steps are commented in the code.

$coordinates = "9.499819 123.920318,9.490845 123.916563,9.484644 123.922292,9.49148 123.931519,9.499755 123.925683,9.499819 123.920318";

$polygon = explode(',',$coordinates);
/*
array (
  0 => "9.499819 123.920318",
  1 => "9.490845 123.916563",
*/

foreach($polygon as $index => $latLng){
  //The element $latLng is split into two variables with list and explode.
  list($lat,$lng) = explode(' ',$latLng);

  //The elements are replaced by a new array with the variables $lat and $lng as float
  $polygon[$index] = ['lat' => (float)$lat, 'lng' => (float)$lng];
}

//test output
echo '<pre>';
var_export($polygon);

output:

array (
  0 => 
  array (
    'lat' => 9.499819,
    'lng' => 123.920318,
  ),
  1 => 
  array (
    'lat' => 9.490845,
    'lng' => 123.916563,
  ),
  2 => 
  array (
    'lat' => 9.484644,
    'lng' => 123.922292,
  ),
  3 => 
  array (
    'lat' => 9.49148,
    'lng' => 123.931519,
  ),
  4 => 
  array (
    'lat' => 9.499755,
    'lng' => 123.925683,
  ),
  5 => 
  array (
    'lat' => 9.499819,
    'lng' => 123.920318,
  ),
) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM