将包含多个经度和纬度的字符串转换为2个数组(一个用于经度,一个用于纬度)


Turn a string of multiple Longitude and Latitudes into 2 arrays (one for Longitude, one for Latitude)

我将传递经度&如下所示多边形的纬度值

[ [ 51.11041991029261, -2.274169921875 ], [ 51.08282186160978, -3.460693359375 ], [ 50.443513052458044, -3.570556640625 ], [ 50.443513052458044, -1.966552734375 ] ]

我需要把这个字符串变成2个数组。1只使用经度值([]左侧的值)和纬度数组([]右侧的值)-或者我总是把它们混在一起。

我尝试过explosion和其他一些PHP函数但是没有成功

这看起来像JSON,所以…

$json = <<<JSON
[ [ 51.11041991029261, -2.274169921875 ], [ 51.08282186160978, -3.460693359375 ], [ 50.443513052458044, -3.570556640625 ], [ 50.443513052458044, -1.966552734375 ] ]
JSON;
$coords = json_decode($json, true);
$lats   = array_map(function (array $coord) { return $coord[0]; }, $coords);
$lons   = array_map(function (array $coord) { return $coord[1]; }, $coords);

$lats = array_column($coords, 0);
$lons = array_column($coords, 1);