sleepyw Posted June 14, 2012 Share Posted June 14, 2012 I've been away from PHP for a bit and thought I was updating something simple until I sat down to write the code, and I'm completely stumped. Here's what I'm trying to do: 1. Search for all codes from a db table - each result has 3 letters followed by 4 numbers (e.g., ABC1234) 2. From those results, each item from that array needs to have the first 3 characters stripped from it, leaving JUST the last 4 numbers 3. I then need to sort all of those results to show ONLY the highest number. For example: $code_search = mysql_query("SELECT Code FROM table"); That will yield an array with results like: "ABC1234", "XYZ9345", "QHD3062", "PVT7655" I need to remove the first three letters from each of those to just show the last 4 numbers, sort those in descending order, and display the highest number, which in the above example would be "9345". Here's the code to show what I'm TRYING to do, which is obviously wrong because I've got a command (substr) trying to strip characters from an array when it needs to be a string....but after searching for an hour, I can't seem to find the right code to remove the letters from the array results. $code_search = mysql_query("SELECT Code FROM table"); $code_results = mysql_fetch_array($code_search); // remove first three letters from above array to only show final 4 numbers $high_code_num = substr ($code_results, 3, 4); // sort above results descending to just show the top result (highest number) $sorted_codes = rsort ($high_code_num); // show just the top result only $top_course_code = $sorted_codes[0]; Ay help for this lost soul? TIA Quote Link to comment https://forums.phpfreaks.com/topic/264147-strip-characters-from-aray-results-reorder-display-top-result/ Share on other sites More sharing options...
scootstah Posted June 14, 2012 Share Posted June 14, 2012 This seems to work (requires PHP>=5.3): $array = array('abc1234', 'def5678', 'ghi9012'); $array = array_map(function($x){ return substr($x, 3, 4); }, $array); echo max($array); Quote Link to comment https://forums.phpfreaks.com/topic/264147-strip-characters-from-aray-results-reorder-display-top-result/#findComment-1353658 Share on other sites More sharing options...
requinix Posted June 14, 2012 Share Posted June 14, 2012 SELECT RIGHT(field, 4) FROM table WHERE field REGEXP "^[A-Z]{3}[0-9]{4}$" ORDER BY RIGHT(field, 4) DESC Quote Link to comment https://forums.phpfreaks.com/topic/264147-strip-characters-from-aray-results-reorder-display-top-result/#findComment-1353659 Share on other sites More sharing options...
sleepyw Posted June 14, 2012 Author Share Posted June 14, 2012 SELECT RIGHT(field, 4) FROM table WHERE field REGEXP "^[A-Z]{3}[0-9]{4}$" ORDER BY RIGHT(field, 4) DESC With a little tweaking, I got it to work! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/264147-strip-characters-from-aray-results-reorder-display-top-result/#findComment-1353778 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.