monkeytooth Posted April 14, 2011 Share Posted April 14, 2011 I've tried various sort methods on this array. And seem to be failing. Array ( [monkey quest] => 8 [monkey] => 2 [monkey sports] => 0 [monkey go happy] => 0 [monkey go happy 3] => 1 [monkey joes] => 0 [monkey games] => 2 [monkey bread recipe] => 1 [monkey go happy 2] => 3 [monkey quest trailer] => 4 [monkey quest guide] => 5 [monkey quest nick] => 6 ) What I want to do or attempt to do rather is sort the array by the => value from highest to lowest (or maybe in reverse as well, but one thing at a time) My last failed attempt I was trying out usort() as my concept.. function cmp($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; } echo "<pre>"; print_r(usort($kw, "cmp")); echo "</pre>"; But the only thing I am left with on the page is just 1 everything else seems to be getting lost, or I dunno whats going on. So I think I've done stumped my self. Now I'm looking for idea's assuming I am tackling this all wrong. Quote Link to comment https://forums.phpfreaks.com/topic/233684-array-sort-fail/ Share on other sites More sharing options...
requinix Posted April 14, 2011 Share Posted April 14, 2011 What does "=> value" mean? The array keys or values? Keys: ksort or krsort Values: asort or arsort + SORT_NUMERIC Quote Link to comment https://forums.phpfreaks.com/topic/233684-array-sort-fail/#findComment-1201444 Share on other sites More sharing options...
monkeytooth Posted April 14, 2011 Author Share Posted April 14, 2011 the array I posted in my example. Thats the actual output from the overall function I have working currently. With that I am I want to sort it by those numeric values so it would go from the original output: Array( [monkey quest] => 8 [monkey] => 2 [monkey sports] => 0 [monkey go happy] => 0 [monkey go happy 3] => 1 [monkey joes] => 0 ) to this output: Array( [monkey quest] => 8 [monkey] => 2 [monkey go happy 3] => 1 [monkey go happy] => 0 [monkey joes] => 0 [monkey sports] => 0 ) or this output: Array( [monkey go happy] => 0 [monkey joes] => 0 [monkey sports] => 0 [monkey go happy 3] => 1 [monkey] => 2 [monkey quest] => 8 ) Quote Link to comment https://forums.phpfreaks.com/topic/233684-array-sort-fail/#findComment-1201449 Share on other sites More sharing options...
monkeytooth Posted April 14, 2011 Author Share Posted April 14, 2011 only thing I can use to get it to work is a "usort" however I lose my values this is what I am doing: function cmp($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; } usort($kw, "cmp"); kw being the array I posted earlier. My Issue is after its sorted I want to take the pairs of data and list them out in that order on the page trying this foreach($kw as $key => $value) { echo $key . " - " . $value ."<br>"; } i get: 0 - 0 1 - 0 2 - 0 3 - 1 4 - 8 what I want is: monkey go happy - 0 monkey joes - 0 monkey sports - 0 monkey go happy 3 - 1 monkey quest - 8 Any idea's how to get that? Ive tried other sort methods to. But none work for me thus far other than usort, and usort really isnt doing what I want it to fully maybe Im doing the function its calling wrong. but I need extra eyes to help me sift through and figure it out. Quote Link to comment https://forums.phpfreaks.com/topic/233684-array-sort-fail/#findComment-1201522 Share on other sites More sharing options...
monkeytooth Posted April 14, 2011 Author Share Posted April 14, 2011 never mind, i got it.. i completely over looked the asort bit, and tried that and presto.. gotta love the little things holding you back for hours on end.. ey.. thanks to all for the help.. Quote Link to comment https://forums.phpfreaks.com/topic/233684-array-sort-fail/#findComment-1201525 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.