Jump to content

Strip characters from aray results, reorder, display top result


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.