If you sort (8, 9, 10, 11, 12), how do you want them to be output? "In the order you've just listed, Graham", you'll probably say. But if they're strings and you sort them, then you'll get the order 10, 11, 12, 8, 9 output if you just use a default sort, because the character "1" comes before the character "8" in the alphabet.
Using PHP, you could write your own sort routine (using usort) ... but there's a better way - a function called "natsort". Remember the PHP rule of thumb - if you think
surely someone's done that before then often the solution will be
there's a function to do that.
Here's an example of the output from an illustrative program:
Original
0 .. 12 Drummers Drumming
1 .. 7 Swans a'Swimming
2 .. 11 Pipers Piping
3 .. 9 Ladies Dancing
4 .. 10 Lords a'Leaping
5 .. 8 Maid a'Milking
Default Sort
0 .. 10 Lords a'Leaping
1 .. 11 Pipers Piping
2 .. 12 Drummers Drumming
3 .. 7 Swans a'Swimming
4 .. 8 Maid a'Milking
5 .. 9 Ladies Dancing
Numeric sort
0 .. 7
1 .. 8
2 .. 9
3 .. 10
4 .. 11
5 .. 12
Natural Sort
1 .. 7 Swans a'Swimming
5 .. 8 Maid a'Milking
3 .. 9 Ladies Dancing
4 .. 10 Lords a'Leaping
2 .. 11 Pipers Piping
0 .. 12 Drummers Drumming
This program can be run from
here and you can view the source code
here.
(written 2004-09-19, updated 2006-06-05)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H999 - Additional PHP Material [239] What and why for the epoch - (2005-03-08)
[320] Ordnance Survey - using a 'Get a map' - (2005-05-22)
[322] More maps - (2005-05-23)
[337] the array returned by preg_match_all - (2005-06-06)
[372] Time calculation in PHP - (2005-07-08)
[468] Stand alone PHP programs - (2005-10-18)
[483] Double Dollars in PHP - (2005-11-02)
[493] Running a Perl script within a PHP page - (2005-11-12)
[563] Merging pictures using PHP and GD - (2006-01-13)
[603] PHP - setting sort order with an associative array - (2006-02-13)
[665] PHP Image viewing application - (2006-04-01)
[687] Presentation, Business and Persistence layers in Perl and PHP - (2006-04-17)
[789] Hot answers in PHP - (2006-07-02)
[806] Check your user is human. Have him retype a word in a graphic - (2006-07-17)
[822] PHP - a team member leaves - (2006-08-04)
[839] Reporting on the 10 largest files or 10 top scores - (2006-08-20)
[917] Syntax checking in PHP - (2006-11-07)
[937] Display an image from a MySQL database in a web page via PHP - (2006-11-22)
[1010] Dates, times, clickable diarys in PHP - (2006-12-28)
[1020] Parallel processing in PHP - (2007-01-03)
[1053] Sorting people by name in PHP - (2007-01-26)
[1104] Drawing dynamic graphs in PHP - (2007-03-09)
[1194] Drawing hands on a clock face - PHP - (2007-05-19)
[1270] PHP Standalone - keyboard to screen - (2007-07-18)
[1389] Controlling and labelling Google maps via PHP - (2007-10-13)
[1390] Converting from postal address to latitude / longitude - (2007-10-13)
[1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
[1451] More PHP sample and demonstration programs - (2007-12-01)
[1485] Copyright and theft of images, bandwidth and members. - (2007-12-26)
[1505] Script to present commonly used images - PHP - (2008-01-13)
[1519] Flipping images on your web page - (2008-01-26)
[1623] PHP Techniques - a workshop - (2008-04-26)
[2073] Extra PHP Examples - (2009-03-09)
[2215] If nothing, make it nothing. - (2009-06-02)
[2684] Exception handling in PHP - (2010-03-18)
[3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
[3210] Catchable fatal error in PHP ... How to catch, and alternative solutions such as JSON - (2011-03-22)
[4655] Image indexer / thumbnail display scripts in PHP - (2016-02-25)
H107 - String Handling in PHP [31] Here documents - (2004-08-28)
[422] PHP Magic Quotes - (2005-08-22)
[463] Splitting the difference - (2005-10-13)
[558] Converting between acres and hectares - (2006-01-08)
[560] The fencepost problem - (2006-01-10)
[574] PHP - dividing a string up into pieces - (2006-01-23)
[589] Robust PHP user inputs - (2006-02-03)
[608] Don't expose your regular expressions - (2006-02-15)
[642] How similar are two words - (2006-03-11)
[716] Evaluating arithmetic expressions in configuration files - (2006-05-10)
[728] Looking ahead and behind in a Regular Expression - (2006-05-22)
[1008] Date conversion - PHP - (2006-12-26)
[1058] PHP Regular expression to extrtact link and text - (2007-01-31)
[1195] Regular Express Primer - (2007-05-20)
[1336] Ignore case in Regular Expression - (2007-09-08)
[1372] A taster PHP expression ... - (2007-09-30)
[1533] Short and sweet and sticky - PHP form input - (2008-02-06)
[1603] Do not SHOUT and do not whisper - (2008-04-06)
[1613] Regular expression for 6 digits OR 25 digits - (2008-04-16)
[1799] Regular Expressions in PHP - (2008-09-16)
[2046] Finding variations on a surname - (2009-02-17)
[2165] Making Regular Expressions easy to read and maintain - (2009-05-10)
[2238] Handling nasty characters - Perl, PHP, Python, Tcl, Lua - (2009-06-14)
[2629] Curly braces within double quoted strings in PHP - (2010-02-09)
[3020] Handling (expanding) tabs in PHP - (2010-10-29)
[3424] Divide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP - (2011-09-08)
[3515] PHP - moving from ereg to preg for regular expressions - (2011-11-11)
[3516] Regular Expression modifiers in PHP - summary table - (2011-11-12)
[3534] Learning to program in PHP - Regular Expression and Associative Array examples - (2011-12-01)
[3788] Getting more than a yes / no answer from a regular expression pattern match - (2012-06-30)
[3789] More than just matching with a regular expression in PHP - (2012-06-30)
[3790] Solution looking for a problem? Lookahead and Lookbehind - (2012-06-30)
[4071] Setting up strings in PHP - (2013-04-27)
[4072] Splitting the difference with PHP - (2013-04-27)
Some other Articles
Call Centres, selling, and a pause for thoughtPosting 1000 letters!Dress CodeEvening classes to learn PHPPHP and natural sortingDrive the driveInternational awarenessGym and swimming - Melksham Blue PoolCurrent cost in your local currencyBusiness is the predominant user of Tomcat, Perl and Tcl