Flames Posted January 18, 2009 Share Posted January 18, 2009 I need to be able to quickly sort an array (key and value kept corresponding), but instead of using letters i want to use numbers, i looked over all the sort functions and krsort seems the most like what i need, a reverse sort of the keys maintaining the value it has but with numbers, so 999 => value1, 1000 => value2, 998 => value3 would become 1000 => value2, 999 => value1, 998 => value3, and if krsort is the right one if i was to use a foreach loop, would the foreach to the array in the sorted order so 1000 first, or would it do it in an normal numeric/alphabetic order 1,2,3,4,5 (a,b,c,d,e) etc Quote Link to comment https://forums.phpfreaks.com/topic/141357-solved-quick-array-sorting-question/ Share on other sites More sharing options...
DarkWater Posted January 18, 2009 Share Posted January 18, 2009 <?php $arr = array(999, 1000, 998); print_r(arr); arsort($arr, SORT_NUMERIC); print_r($arr); ?> Quote Link to comment https://forums.phpfreaks.com/topic/141357-solved-quick-array-sorting-question/#findComment-739875 Share on other sites More sharing options...
Flames Posted January 18, 2009 Author Share Posted January 18, 2009 This works with krsort presumably? never mind, flipped the array around. Quote Link to comment https://forums.phpfreaks.com/topic/141357-solved-quick-array-sorting-question/#findComment-739878 Share on other sites More sharing options...
.josh Posted January 18, 2009 Share Posted January 18, 2009 What is the point in asking whether something will work or not? Just try it. $array = array(999 => 'value1', 1000 => 'value2', 998 => 'value3'); krsort($array); foreach($array as $key => $val) { echo "$key => $val <br/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/141357-solved-quick-array-sorting-question/#findComment-739880 Share on other sites More sharing options...
DarkWater Posted January 18, 2009 Share Posted January 18, 2009 This works with krsort presumably? never mind, flipped the array around. I used arsort(), which maintains key=>value pairs and sorts by value. Quote Link to comment https://forums.phpfreaks.com/topic/141357-solved-quick-array-sorting-question/#findComment-739881 Share on other sites More sharing options...
Flames Posted January 18, 2009 Author Share Posted January 18, 2009 cause i did try it and it didnt sort it numericly Quote Link to comment https://forums.phpfreaks.com/topic/141357-solved-quick-array-sorting-question/#findComment-739882 Share on other sites More sharing options...
.josh Posted January 18, 2009 Share Posted January 18, 2009 really because when I run the code I posted I get 1000 => value2 999 => value1 998 => value3 Is that not what you want? Quote Link to comment https://forums.phpfreaks.com/topic/141357-solved-quick-array-sorting-question/#findComment-739886 Share on other sites More sharing options...
.josh Posted January 18, 2009 Share Posted January 18, 2009 This works with krsort presumably? never mind, flipped the array around. I used arsort(), which maintains key=>value pairs and sorts by value. Unless I'm mistaken he wants to sort by key... Quote Link to comment https://forums.phpfreaks.com/topic/141357-solved-quick-array-sorting-question/#findComment-739887 Share on other sites More sharing options...
Flames Posted January 18, 2009 Author Share Posted January 18, 2009 It is what i wanted, but i tested it and it didnt work for me, maybe different php settings? And sorting by value/key isnt a problem i just wanted to know how to sort numericly. Quote Link to comment https://forums.phpfreaks.com/topic/141357-solved-quick-array-sorting-question/#findComment-739889 Share on other sites More sharing options...
DarkWater Posted January 18, 2009 Share Posted January 18, 2009 Oh, got it backwards then. CV's got it. Quote Link to comment https://forums.phpfreaks.com/topic/141357-solved-quick-array-sorting-question/#findComment-739893 Share on other sites More sharing options...
Flames Posted January 18, 2009 Author Share Posted January 18, 2009 Im good enough at php to be able to change the sorting type of the array Quote Link to comment https://forums.phpfreaks.com/topic/141357-solved-quick-array-sorting-question/#findComment-739899 Share on other sites More sharing options...
.josh Posted January 18, 2009 Share Posted January 18, 2009 It is what i wanted, but i tested it and it didnt work for me, maybe different php settings? And sorting by value/key isnt a problem i just wanted to know how to sort numericly. sorting numerically versus...what? Quote Link to comment https://forums.phpfreaks.com/topic/141357-solved-quick-array-sorting-question/#findComment-739901 Share on other sites More sharing options...
Flames Posted January 18, 2009 Author Share Posted January 18, 2009 sorting alphabetically which is what it did. BTW CV liking the sig, dynamic images ftw. Quote Link to comment https://forums.phpfreaks.com/topic/141357-solved-quick-array-sorting-question/#findComment-739903 Share on other sites More sharing options...
.josh Posted January 18, 2009 Share Posted January 18, 2009 sorting alphabetically which is what it did. BTW CV liking the sig, dynamic images ftw. I think you are misunderstanding. There is no such thing as sorting alphabetically vs. numerically. I think you are talking about sorting by key vs. value. Quote Link to comment https://forums.phpfreaks.com/topic/141357-solved-quick-array-sorting-question/#findComment-739904 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.