I suggest to use the following method.
0) Fetch db data into 2d array looks like this:
array(
array($x1,$y1),
array($x2,$y2),
...
)
1) Build array storing for each point sum of (square) distances to all other points.
2) Find the lowest value in that array (minimum).
3) Get all points having that value.
So the code should look like this:
// $a_points = array( array($x1,$y1), array($x2,$y2), ... ); // array of point coordinates
$a_sum_dists = array();
foreach ($a_points as $i => $pt1) {
list($x1, $y1) = $pt1;
$sum = 0;
foreach ($a_points as $j => $pt2) {
if ($j == $i) continue;
list($x2, $y2) = $pt2;
$sum += pow($x2- $x1, 2) + pow($y2- $y1, 2);
}
$a_sum_dists[$i] = $sum;
}
$min_sum = min($a_sum_dists);
$a_result_points = array_intersect_key(
$a_points,
array_filter($a_sum_dists, function ($v) use ($min_sum) {
return $v == $min_sum;
})
);