dingus Posted April 25, 2008 Share Posted April 25, 2008 ok basicly i have an array i need to sort that from the center out (0.5) and the order descending for example 0.5 0.7 0.1 given that 0.7 is only 2 away from 5 where 0.1 is 4 now to best of my knowledge usort would be the best way to do this but i have no idea how can anyone offer some light on this? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 25, 2008 Share Posted April 25, 2008 Not sure that's possible with a usort function. My approach would be to cycle though the array, and calculate a distance from the centre and store this in an array. Associatively sort this array, then cycle through this array using the keys to put a new array in order: <?php $array = array(0.5,0.7,0.4,0.45,0.1,0.99); foreach($array as $k=>$v){ $temp[$k] = abs(0.5-$v); } asort($temp); foreach($temp as $k=>$v){ $sorted[] = $array[$k]; } echo '<pre>'.print_r($sorted,1).'</pre>'; ?> Quote Link to comment Share on other sites More sharing options...
dingus Posted April 25, 2008 Author Share Posted April 25, 2008 GingerRobot your a life saver this has been stressing me out all day thanks heaps Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.