Jump to content

Strip characters from aray results, reorder, display top result


sleepyw

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.