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 ) )

demo:https://eval.in/398469


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -