Something like
$stars = array(
'star1' => array('x' => 50, 'y' => 50, 'min' => 999),
'star2' => array('x' => 60, 'y' => 30, 'min' => 999),
'star3' => array('x' => 90, 'y' => 110, 'min' => 999),
'star4' => array('x' => 40, 'y' => 40, 'min' => 999),
);
function dist($x1, $y1, $x2, $y2) {
return floor(sqrt((($x2 - $x1) * ($x2 - $x1)) + (($y2 - $y1) * ($y2 - $y1))));
}
function minsort($a,$b) {
if ($a['min'] == $b['min']) return 0;
return ($a['min'] > $b['min']) ? -1 : 1;
}
# find distances to nearest neighbour
$copy = $stars;
foreach ($stars as $k1 => $data1) {
foreach ($copy as $k2 => $data2) {
if ($k1 != $k2) {
$d = dist($data1['x'], $data1['y'],$data2['x'], $data2['y']);
$stars[$k1]['min'] = min($stars[$k1]['min'], $d);
}
}
}
#use custom sort function minsort() to put remotest first
uasort ($stars, 'minsort');
echo '<pre>';
print_r ($stars);
echo '</pre>';
hth