
In any substantial programming task, you'll want to store a whole series of values in some sort of table, so that you can process / reprocess / handle each of the elements in turn - and to do that, you'll use a variable construct that's called an "array", or a "list", a "tuple" or a "vector". (The exact word depends on the particular language you're using ... and how it's been implemented internally).
Arrays, lists, tuples and vectors are what we call "ordered collections" - in other words, they're bundles of variables which are keyed / looked up by position numbers. The numbering usually starts at 0 (exceptions - Lua and Fortran which start at 1), and in many cases that's ideal - but not in all cases.
Imagine that I have a data set which shows the UK ports from which you can take a ferry over the English Channel, and their destinations -
[here]. The sort of question I'll be asking is "where can I go from Portsmouth" and if I keep the data in an array or list - keyed by position number - I'll have to do a search of some sort every time I make an inquiry; chances are that I'll need two arrays which have to be kept in step. Ironically, my user really won't care if Portsmouth is at position 0, 1, 2, or 3 ... nor if Newhaven comes first ...
There has to be a better way!
There is. It's called an "Associative Array" (PHP), a "Dictionary" (Python), a "Hash" (Perl), (a table in Lua, a Hash, Hashmap or hashset in Java ...). In dictionaries and hashes, the elements are not orderable but can be looked up by key. There's a source code example (in Perl)
[here] showing the setup and use of a hash for looking up our channel ports from our channel file - and we cover this in detail on our
Learning to program in Perl course.
In PHP, things are a bit different. The Associative array CAN be ordered (sorted) - which overcomes one of the limitations of hashes and dictionaries, at the expense of efficiency on enormous data tables. But since PHP is going to be used primarily for web page generation, where the time taken / data sent can't be huge, this loss of efficiency in exceptional cases is no big deal. What does this mean?
It means that in PHP, I can set up a table of data - key, value pairs. I can then sort that table, and output the results in order. And I can do it VERY EASILY! I wrote an example of this - using fictional data for the number of deer in various national parks in the UK - on yesterday's
PHP course, and I've shared the source code
[here]. The code is robust (it has to be if it's going to be run on a public web site!) and you can run it
[here].
(written 2010-08-11, updated 2010-08-20)
Associated topics are indexed under
Q101 - Object Orientation and General technical topics - Programming Principles [3551] Some terms used in programming (Biased towards Python) - (2011-12-12)
[3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
[3542] What order are operations performed in, in a Perl expression? - (2011-12-07)
[3456] Stepping stones - early coding, and writing re-usable code quickly - (2011-09-24)
[3026] Coding efficiency - do not repeat yourself! - (2010-11-02)
[2964] An introduction to file handling in programs - buffering, standard in and out, and file handles - (2010-09-21)
[2878] Program for reliability and efficiency - do not duplicate, but rather share and re-use - (2010-07-19)
[2769] Easy - but for whom? - (2010-05-18)
[2737] Improving your function calls (APIs) - General and PHP - (2010-04-24)
[2586] And and Or illustrated by locks - (2010-01-17)
[2550] Do not copy and paste code - there are much better ways - (2009-12-26)
[2510] The music of the stock market - (2009-11-22)
[2415] Variable names like i and j - why? - (2009-09-22)
[2327] Planning! - (2009-08-08)
[2310] Learning to write high quality code in Lua - (2009-07-30)
[2228] Where do I start when writing a program? - (2009-06-11)
[2022] Pre and post increment - the ++ operator - (2009-02-03)
[2001] I have not programmed before, and need to learn - (2009-01-19)
P211 - Perl - Hashes [3451] Why would you want to use a Perl hash? - (2011-09-20)
[3400] $ is atomic and % and @ are molecular - Perl - (2011-08-20)
[3106] Buckets - (2010-12-26)
[3072] Finding elements common to many lists / arrays - (2010-11-26)
[3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
[2920] Sorting - naturally, or into a different order - (2010-08-14)
[2836] Perl - the duplicate key problem explained, and solutions offered - (2010-06-28)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[1917] Out of memory during array extend - Perl - (2008-12-02)
[1856] A few of my favourite things - (2008-10-26)
[1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
[1705] Environment variables in Perl / use Env - (2008-07-11)
[1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
[968] Perl - a list or a hash? - (2006-12-06)
[930] -> , >= and => in Perl - (2006-11-18)
[738] (Perl) Callbacks - what are they? - (2006-05-30)
[386] What is a callback? - (2005-07-22)
[240] Conventional restraints removed - (2005-03-09)
Y107 - Python - Dictionaries [3555] Football league tables - under old and new point system. Python program. - (2011-12-18)
[3554] Learning more about our web site - and learning how to learn about yours - (2011-12-17)
[3488] Python sets and frozensets - what are they? - (2011-10-20)
[3464] Passing optional and named parameters to python methods - (2011-10-04)
[2994] Python - some common questions answered in code examples - (2010-10-10)
[2986] Python dictionaries - reaching to new uses - (2010-10-05)
[2368] Python - fresh examples of all the fundamentals - (2009-08-20)
[1145] Using a list of keys and a list of values to make a dictionary in Python - zip - (2007-04-13)
[1144] Python dictionary for quick look ups - (2007-04-12)
[955] Python collections - mutable and imutable - (2006-11-29)
[103] Can't resist writing about Python - (2004-10-29)
H106 - PHP - Arrays [3534] Learning to program in PHP - Regular Expression and Associative Array examples - (2011-12-01)
[3379] Sorting data the way YOU want it sorted - (2011-08-05)
[3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
[2274] PHP preg functions - examples and comparision - (2009-07-08)
[2215] If nothing, make it nothing. - (2009-06-02)
[1614] When an array is not an array - (2008-04-17)
[1451] More PHP sample and demonstration programs - (2007-12-01)
[1199] Testing for one of a list of values. - (2007-05-22)
[1116] PHP adding arrays / summing arrays - (2007-03-23)
[832] Displaying data at 5 items per line on a web page - (2006-08-14)
[773] Breaking bread - (2006-06-22)
[603] PHP - setting sort order with an associative array - (2006-02-13)
[409] Functions and commands with dangerous names - (2005-08-11)
Some other Articles
London to Calne, Corsham, Melksham, Bradford-on-Avon, Chippenham by public TransportDownloading a report from the web for further local analysisUpload Image, Store in database, redisplay in browser. PHP and MySQLTesting the robustness of our hotel and training systems - holiday and sickness timesLooking up a value by key - associative arrays / Hashes / DictionariesOlder picture.Six languages in one file - an HTML++ web pagePredictions for the seagull populationFeeding the GrocklesRobust - testing the system