Help for server bottleneck on sorting array

hi i wana sort an Multidimensional Arrays on my server,i think its will down my server,u have any piont how to do this in best way?any alghoritm for doing this?

PHP has quite a few array functions, including array_multisort

Why do you think it would crash your server?

“best” depends entirely on what you have and what you want to do with it.

1 Like

BECAUSE SORTING ARRAYS IS N LOG N ,AND ITS TAKE MANY TIME TO SORT,

I WANA USE LATITUTE AND LONGTITUTE for find post near of user,i have function to calculate distance,and its that function

function distance($lat1, $lon1, $lat2, $lon2) {
  $theta = $lon1 - $lon2;
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  $dist = acos($dist);
  $dist = rad2deg($dist);
  $miles = $dist * 60 * 1.1515;
  return ($miles * 1.609344); 
}
/*echo distance(32.9697, -96.80322, 29.46786, -98.53506) . " Kilometers<br>";*/
echo distance(32.9697, -96.80322, 29.46786, -98.53506);
echo "<br>";
echo distance($yes[0], $yes2[0], $yes[1], $yes2[1]);

and i use this query to select all latt and longs from post table
$sql=“select * from post2”;
$res=mysqli_query($con, $sql);

then i need to calculate distance with this while ($ray=mysqli_fetch_assoc($res)) {

and i use distance function to calculate distance for each point then i need to save them in array and sort them and send them to android app,

is it clear what i wana do?

Thanks, I think I understand (please correct me if I’m wrong)

You get Lats and Longs from the database and put into an array. eg. (over simplified numbers)

1 44 59
2 -73 21
3 86 -43
etc. etc.
then you get
newuser 37 62
and calculate the distance from newuser to each of those from the database
then you want to sort by distance.

1 Like

yes,exactly

I’m not seeing how an array of distances is multidimensional. wouldn’t it be a simple array like

0 => 12 miles
1 => 35 miles
2 => 27 miles 

I’m thinking the real bottleneck is getting that.

I have a feeling a complex query would be the better approach

I’m having trouble thinking how to deal +/- values (i.e. north/south, east/west hemispheres) ATM

Maybe @felgall knows the Maths?

1 Like

multidimentional array is include the distance and id and post content and post tiltle and picture link,

i think complex query is better too but i m not good in sql…and i dont know how to do it,
,

abs() seems to work OK

echo abs( (73.6823) - (24.3478) ); // 49.3345
echo "<br />";
echo abs( (24.3478) - (73.6823) ); // 49.3345
echo "<br />";
echo abs( (-24.3478) - (-73.6823) ); // 49.3345
echo "<br />";
echo abs( (-24.3478) - (24.9867) ); // 49.3345
1 Like

thanks so much,
with abs() function we returns the absolute (positive) value of a number.
so what is next step? do you wana change distance function and make complex query,how?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.