我有一个这样的数组,x坐标和y坐标形成一个单独的条目。
$polygon = array("10 0", "20 5", "15 15", "22 15");
现在我如何将这个数组分成2个不同的数组,这样所有的x坐标都会落入一个数组,所有的y坐标都会落入另一个数组,如下所示:
$x = array(10, 20, 15, 22);
$y = array(0, 5, 15, 15);
答案 0 :(得分:5)
$x = $y = array();
$polygon = array("10 0", "20 5", "15 15", "22 15");
foreach ($polygon as $coor) {
list($x[], $y[]) = explode(' ', $coor);
}
这样可以解决问题:)
并将它们组合起来:
//assuming that $x and $y have the same number of items
for ($i = 0; $i<count($x); $i++) {
$polygon[] = $x[$i] .' ' . $y[$i];
}
答案 1 :(得分:1)
试试:
$x = array();
$y = array();
foreach($polygon as $entry){
$splitted = explode(" ", $entry);
//append x and y
$x[] = $splitted[0];
$y[] = $splitted[1];
}