0

I want to sort the elements in an array (containing latitude and longitude) in comparison to some reference points latitude and longitude. (By distance) The reference point is known just right before calling the sort function. Is there any way to enter three parameters in the callback-function of the usort() function or do I need to write me own complete sort function? My plan was something like that:

$referencePlace = fetchReferencePlace();
$relevantPlaces = fetchRelevantPlaces();
usort($relevantPlaces, sortByDistance($a, $b, $referencePlace)); 

Note: This question is a duplicate of: Pass extra parameters to usort callback

  • What is the purpose of this self-answered question? This principle is already offered in an [answer](https://stackoverflow.com/a/22610655/4205384) to a question titled "Pass extra parameters to usort callback". This constitutes duplicate content. – El_Vanja Apr 27 '21 at 15:44

1 Answers1

1

Apparently this is possible, when you are using an anonymous function. The code looks like the following then:

$referencePlace = fetchReferencePlace();
$relevantPlaces = fetchRelevantPlaces();
usort($relevantPlaces, function($a,$b) use ($referencePlace) {
//Insert your code doing the comparison here.
});