Alicia Posted August 11, 2011 Share Posted August 11, 2011 Hi, May I know is there any function that I can use to have 3 digits number re-arranged to small to big ? something like 762 then the output will be 267. Please advise. thanks Quote Link to comment https://forums.phpfreaks.com/topic/244477-sorting/ Share on other sites More sharing options...
trq Posted August 11, 2011 Share Posted August 11, 2011 You would need to right a custom function for this. It's not really that common a request. Quote Link to comment https://forums.phpfreaks.com/topic/244477-sorting/#findComment-1255752 Share on other sites More sharing options...
manix Posted August 11, 2011 Share Posted August 11, 2011 <?php function whatever($input){ $input = (string)$input; $input = substr($input,-1,1).substr($input,-2,1).substr($input,0,1); return (int)$input; } echo whatever(762); ?> Quote Link to comment https://forums.phpfreaks.com/topic/244477-sorting/#findComment-1255760 Share on other sites More sharing options...
cyberRobot Posted August 11, 2011 Share Posted August 11, 2011 <?php function whatever($input){ $input = (string)$input; $input = substr($input,-1,1).substr($input,-2,1).substr($input,0,1); return (int)$input; } echo whatever(762); ?> That's not quite what the OP was asking. It works for the specific value mentioned, but it doesn't work if the input is "726" for example. Instead, you could try something like: <?php function whatever($input){ //INITIALIZE VARIABLES $num_array = array(); //BREAK APART THE INPUT STRING for($i=0; $i<strlen($input); $i++) { $num_array[] = substr($input, $i, 1); } //SORT THE ARRAY AND RETURN THE RESULTS sort($num_array); return implode($num_array); } echo whatever($_GET['input']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/244477-sorting/#findComment-1255822 Share on other sites More sharing options...
Alex Posted August 11, 2011 Share Posted August 11, 2011 Instead of using a for loop and substr, alternatively you can take advantage of str_split, my take: function dontKnowWhatToNameThis($n) { $split = str_split($n); sort($split); return implode($split); } Quote Link to comment https://forums.phpfreaks.com/topic/244477-sorting/#findComment-1255850 Share on other sites More sharing options...
TeNDoLLA Posted August 11, 2011 Share Posted August 11, 2011 Instead of using a for loop and substr, alternatively you can take advantage of str_split, my take: function dontKnowWhatToNameThis($n) { $split = str_split($n); sort($split); return implode($split); } Was going to post exactly the same earlier, but assumed the manix's version worked just fine when I tested it with only the given test value Good feature in this method is also that the number can be longer or shorter than 3 digits and it still works. Quote Link to comment https://forums.phpfreaks.com/topic/244477-sorting/#findComment-1255898 Share on other sites More sharing options...
manix Posted August 11, 2011 Share Posted August 11, 2011 <?php function whatever($input){ $input = (string)$input; $input = substr($input,-1,1).substr($input,-2,1).substr($input,0,1); return (int)$input; } echo whatever(762); ?> this is supposed to work with any 3 digit input? Quote Link to comment https://forums.phpfreaks.com/topic/244477-sorting/#findComment-1256022 Share on other sites More sharing options...
TeNDoLLA Posted August 11, 2011 Share Posted August 11, 2011 Nopes, if you try e.g value 726 it will output: 627 instead of 267. It will only flip the numbers around, not arrange them from lowest to highest. Quote Link to comment https://forums.phpfreaks.com/topic/244477-sorting/#findComment-1256027 Share on other sites More sharing options...
manix Posted August 11, 2011 Share Posted August 11, 2011 well isn't that the point? displaying them backwards? EDIT: I just read the first post again, my bad sorry Quote Link to comment https://forums.phpfreaks.com/topic/244477-sorting/#findComment-1256030 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.