我将变量点数的多边形多坐标存储在MySQL DB上,格式为:
(73.34545, 22.58899),
(73.34567, 22.55656),
(73.34356, 22.51233),
(73.34123, 22.52445)我想用之前的xy坐标创建一个如下形式的多边形数组:
$polygon = array(
new Point(73.34545,22.58899),
new Point(73.34567,22.55656),
new Point(73.34356,22.51233),
new Point(73.34123,22.52445)
);发布于 2015-09-01 11:52:04
我认为你正在寻找的东西是这样的:
class Point {
private $x;
private $y;
public function __construct($x,$y) {
$this->x = $x;
$this->y = $y;
}
public function get_coordinates() {
return array($x,$y);
}
}
$polygon = array( new Point(73.34545,22.58899), new Point(73.34567,22.55656), new Point(73.34356,22.51233), new Point(73.34123,22.52445) );发布于 2015-09-01 14:13:46
虽然我还没有尝试执行下面的代码,但希望您能理解我想要做的事情。
$database_value = (73.34545, 22.58899), (73.34567, 22.55656), (73.34356, 22.51233), (73.34123, 22.52445);
$co_ordinates = explode(",",$database_value);
foreach($co_ordinates as $co_ordinate){
$co_ordinate = str_replace("(","",$co_ordinate);
$co_ordinate = str_replace(")","",$co_ordinate);
$co_ordinate_array = explode(",", $co_ordinate);
$polygon[] = new Point($co_ordinate_array[0],$co_ordinate_array[1]);
}发布于 2015-09-16 19:06:49
@user2985035尝试更新后的代码
$database_value = "(73.34545, 22.58899), (73.34567, 22.55656), (73.34356, 22.51233), (73.34123, 22.52445)";
$co_ordinates_stripped = explode("), (", $database_value);
$co_ordinates = str_replace(")", "", str_replace("(", "", $co_ordinates_stripped));
foreach ($co_ordinates as $co_ordinate) {
$co_ordinate = str_replace("(", "", $co_ordinate);
$co_ordinate = str_replace(")", "", $co_ordinate);
$co_ordinate_array = explode(",", $co_ordinate);
$polygon[] = new Point($co_ordinate_array[0], $co_ordinate_array[1]);
}https://stackoverflow.com/questions/32323184
复制相似问题