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

时间:2015-09-01 03:43:36

标签: php arrays

我有以MySQL形式存储在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)
);

3 个答案:

答案 0 :(得分:0)

我认为你正在寻找这样的东西:

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

答案 1 :(得分:0)

虽然我没有尝试执行以下代码,但希望您能理解我想要做的事情。

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

答案 2 :(得分:0)

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