我有一个像这样的数组,x坐标和y坐标形成一个单独的条目.
$polygon = array("10 0", "20 5", "15 15", "22 15");
Run Code Online (Sandbox Code Playgroud)
现在我怎么能将这个数组分成2个不同的数组,这样所有的x坐标都会落入一个数组,所有的y坐标都会落入另一个数组,如下所示:
$x = array(10, 20, 15, 22);
$y = array(0, 5, 15, 15);
Run Code Online (Sandbox Code Playgroud)
$x = $y = array();
$polygon = array("10 0", "20 5", "15 15", "22 15");
foreach ($polygon as $coor) {
list($x[], $y[]) = explode(' ', $coor);
}
Run Code Online (Sandbox Code Playgroud)
这样做诀窍:)
并将它们组合起来:
//assuming that $x and $y have the same number of items
for ($i = 0; $i<count($x); $i++) {
$polygon[] = $x[$i] .' ' . $y[$i];
}
Run Code Online (Sandbox Code Playgroud)