Sorting values from multiple arrays

Hello,

In PHP, i am getting 3 values from a DB and putting them into an array. They are ID, Title and Distance. The distance is being calculated in PHP because it is longitude and latitude. I am then sorting using asort() on the arrayDistance however this doesn't sort the ID and Title at the same time if you get what i mean. I need it so take all 3 arrays and sort by arrayDistance. Sorting arrayDistance alone muddles up the other arrays and no longer can i do $arrayTitle[0] $arrayID[0] and $arrayDistance[0].

Thanks in advance.
$i=0;
while ($i < $num) {
	$near_title=mysql_result($result,$i,"title");
	$near_long=mysql_result($result,$i,"Longitude");
	$near_lat=mysql_result($result,$i,"Latitude");
	$near_id=mysql_result($result,$i,"ID");
	
	$arrayTitle[$i]=$near_title;
	$arrayID[$i]=$near_id;
	$arrayDistance[$i]=round(distance($spot_lat, $spot_long, $near_lat, $near_long, "m"), 1);
$i++;
}

asort($arrayDistance);

Open in new window

previewservicesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

te-eduCommented:
$i=0;
$array = array();
while ($i < $num) {
	$near_title=mysql_result($result,$i,"title");
	$near_long=mysql_result($result,$i,"Longitude");
	$near_lat=mysql_result($result,$i,"Latitude");
	$near_id=mysql_result($result,$i,"ID");
	
	$array[$i]['title']=$near_title;
	$array[$i]['id']=$near_id;
	$array[$i]['distance']=round(distance($spot_lat, $spot_long, $near_lat, $near_long, "m"), 1);
$i++;
}

asort($array);

Open in new window


Accessing:
$array[0]['title'] = 
$array[0]['id'] = 
$array[0]['distance'] = 

Open in new window

previewservicesAuthor Commented:
Okay thanks. But where do i say sort by distance?  I only see asort($array);
Ray PaseurCommented:
Make an array of objects.  Use the distance as the keys and you can use ksort() to sort the data.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

previewservicesAuthor Commented:
Have you got an example please?
te-eduCommented:

$distance = round(distance($spot_lat, $spot_long, $near_lat, $near_long, "m"), 1);

$array[$i][$distance]['distance']=$distance;
$array[$i][$distance][title]=$near_title;
$array[$i][$distance]['id']=$near_id;

ksort($array);

Open in new window


mattibuttCommented:
hi
why dont you write your sql query in a manner where they appear sorted it will be much easier

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.