如何从存储的数据库多坐标创建一个php多边形数组



我有变量点数的多边形多坐标存储在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)
);

我想你在找这样的东西:

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

虽然我没有尝试执行下面的代码,但希望你能理解我想做什么。

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

@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]);
    }

最新更新