Vivid Lust Posted February 17, 2010 Share Posted February 17, 2010 Hi all, How could i sort this array so that: index text count 1 england 2 2 scotland 5 3 wales 1 becomes: index text count 2 scotland 5 1 england 2 3 wales 1 Thanks all!! Quote Link to comment https://forums.phpfreaks.com/topic/192402-array-sort/ Share on other sites More sharing options...
sasa Posted February 17, 2010 Share Posted February 17, 2010 try <?php $test = array(1 => array('text' => 'england', 'count' => 2), array('text' => 'scotland', 'count' => 5), array('text' => 'wales', 'count' => 1) ); echo '<pre>',print_r($test), '</pre><hr />'; function my_compare($a, $b){ if ($a['count'] == $b['count']) return 0; return $a['count'] < $b['count'] ? 1 : -1; } uasort($test, 'my_compare'); echo '<pre>',print_r($test), '</pre><hr />'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/192402-array-sort/#findComment-1013914 Share on other sites More sharing options...
teamatomic Posted February 17, 2010 Share Posted February 17, 2010 foreach ($array as $key => $row) { $count[$key] = $row['count']; } array_multisort($count, SORT_DESC, $array); HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/192402-array-sort/#findComment-1014007 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.