Sorting Associative Array in PHP -
struggling sort array:
it database driven gets added so:
$distancearray[] = array($attractionid => $tempdistance);
and output using pre tags following:
array ( [0] => array ( [4] => 114.4 ) [1] => array ( [3] => 16.1 ) [2] => array ( [2] => 15 ) [3] => array ( [1] => 21.4 ) )
i've tried no luck:
function cmp($a, $b) { return $a['tempdistance'] - $b['tempdistance']; } usort($distancearray, "cmp");
and krsort w3 schools
try this...
$test = array( array(4=>"114.4"), array(3=>"16.1"), array(2=>"15"), array(1=>"21.4"), ); $value = array(); foreach ($test $key => $row) { $value[$key] = array_values($row); } array_multisort($value, sort_asc,$test); print_r($value);
answer:array ( [0] => array ( [0] => 15 ) [1] => array ( [0] => 16.1 ) [2] => array ( [0] => 21.4 ) [3] => array ( [0] => 114.4 ) )
Comments
Post a Comment