Sorting an array by a key value..

I am having some trouble sorting an array into the order that I want..  It looks like this..

$results = array();
$results[0] = array (
"table"=>"String",
"total"=>"String",
"data"=>array ("some", "data", "for", "multidimensional array");,
"distance"=>44 );

$results[1] = array (
"table"=>"String",
"total"=>"String",
"data"=>array ("some", "data", "for", "multidimensional array");,
"distance"=>30 );

$results[2] = array (
"table"=>"String",
"total"=>"String",
"data"=>array ("some", "data", "for", "multidimensional array");,
"distance"=>40 );

I would like the array to be sorted by distance, shortest to longest.  How can I go about ding that?

Thanks for any help!
graphxdivaAsked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
try:
<?php
function cmp($a, $b)
{
    return strcmp($a["distance"], $b["distance"]);
}
$results = array();
$results[0] = array (
"table"=>"String",
"total"=>"String",
"data"=>array ("some", "data", "for", "multidimensional array");,
"distance"=>44 );

$results[1] = array (
"table"=>"String",
"total"=>"String",
"data"=>array ("some", "data", "for", "multidimensional array");,
"distance"=>30 );

$results[2] = array (
"table"=>"String",
"total"=>"String",
"data"=>array ("some", "data", "for", "multidimensional array");,
"distance"=>40 );


usort($results, "cmp");

...
?>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.