Lets take the example of a polygon, with its vertices forming an array.
<?php
$x = $y = array();
// An array representing a polygon
$polygon = array("10 0", "20 5", "15 15", "22 15");
print_r($polygon);
echo '<br>';
// Break this into 2 arrays, with x-coordinates and y-coordinates forming individual arrays
foreach ($polygon as $coor) {
list($x[], $y[]) = explode(' ', $coor);
}
print_r($x);
echo '<br>';
print_r($y);
echo '<br>';
// Now combine these arrays to get back the polygon array
for ($i = 0; $i<count($x); $i++) {
$poly[] = $x[$i] .' ' . $y[$i];
}
print_r($poly);
?>
Output:
Array ( [0] => 10 0 [1] => 20 5 [2] => 15 15 [3] => 22 15 )
Array ( [0] => 10 [1] => 20 [2] => 15 [3] => 22 )
Array ( [0] => 0 [1] => 5 [2] => 15 [3] => 15 )
Array ( [0] => 10 0 [1] => 20 5 [2] => 15 15 [3] => 22 15 )
<?php
$x = $y = array();
// An array representing a polygon
$polygon = array("10 0", "20 5", "15 15", "22 15");
print_r($polygon);
echo '<br>';
// Break this into 2 arrays, with x-coordinates and y-coordinates forming individual arrays
foreach ($polygon as $coor) {
list($x[], $y[]) = explode(' ', $coor);
}
print_r($x);
echo '<br>';
print_r($y);
echo '<br>';
// Now combine these arrays to get back the polygon array
for ($i = 0; $i<count($x); $i++) {
$poly[] = $x[$i] .' ' . $y[$i];
}
print_r($poly);
?>
Output:
Array ( [0] => 10 0 [1] => 20 5 [2] => 15 15 [3] => 22 15 )
Array ( [0] => 10 [1] => 20 [2] => 15 [3] => 22 )
Array ( [0] => 0 [1] => 5 [2] => 15 [3] => 15 )
Array ( [0] => 10 0 [1] => 20 5 [2] => 15 15 [3] => 22 15 )
Comments
Post a Comment