For 2023 - we are now fully retired from IT training. We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.
Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here)) |
PHP module H106
Arrays
Exercises, examples and other material relating to training module H106. This topic is presented on public courses Learning to program in PHP, PHP Programming, Beginning PHP - weekend course / hobby / club / leisure users, PHP Programming, Learning to program in PHP
Arrays are PHP variables into which you can save more than one value. You can then use a loop to process each individual value, you can refer to individual values by using a special syntax, or you can perform operations on the array as a whole.
Articles and tips on this subject | updated | 4244 | Disambiguation - PHP List PHP Lists, or PHPList?
1. Lists in PHP
A PHP "array" - as they're know - is really a linked list in computer science terms [plus an ordered map - another story!]
Let me explain - a linked list is a series of values, one after another held in a single collection, and referenced by a position number ... | 2014-03-07 | 4068 | Arrays in PHP - contain different and even mixed data types PHP's arrays can be keyed by both value and by string... and on this week's PHP course we've covered both. An indexed array (not really an array - rather akin to a list in Perl or Python) starts at index element number 0 and works up from there. Each array member may contain a different type of data, ... | 2013-05-04 | 4072 | Splitting the difference with PHP There are lots of ways of splitting a string into pieces - you can do it character by characters, at a specific character, and word by word. PHP provides functions for splitting strings in these ways - and more - into arrays of shorter strings.
To split a string into a list of single character strings:
$first ... | 2013-04-27 | 1451 | More PHP sample and demonstration programs It's amazing how many little (and not so little) new programs and demonstrations I write - here are some from the last couple of days:
• The Melksham / Bowerhill Christmas Quiz, 2007. Ten questions, work out the answers, click on a link at the bottom the page and it will be refreshed to tell you ... | 2013-01-02 | 3534 | Learning to program in PHP - Regular Expression and Associative Array examples There's a lot to learn on a week-long PHP course - especially if you're new to programming. And delegates need practice during and after the course with some relatively straightforward (but rewarding) real applications. One of the things that we do ourselves is to analyse server log files - allowing ... | 2011-12-03 | 3379 | Sorting data the way YOU want it sorted Pieces of program code ... subroutines, functions, macros, procedures, user commands, methods ... are stored with a name, and within many programming languages are actually stored in the same memory area and under the same structure as other named values - variables. This means that you can pass in ... | 2011-08-05 | 3004 | Increment operators for counting - Perl, PHP, C and others Look at this Perl statement:
$counter = $counter + 1;
"Take the value in $counter, add 1 to it, and put it back in $counter". It's a common programing requirement - indeed, so common that you can write it in a shorter form in many languages:
$counter += 1;
"Add one to the value ... | 2010-10-20 | 2915 | Looking up a value by key - associative arrays / Hashes / Dictionaries 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 ... | 2010-08-20 | 2920 | Sorting - naturally, or into a different order There's a natural sort order for many things - for numbers, it's ascending, for words it's a dictionary order, for names it's by surname. But sometimes you want to sort a list of things different, or there's no provided way within a programming language to apply the natural sort to your type of thing.
In ... | 2010-08-20 | 1199 | Testing for one of a list of values. Question [PHP] When you have a string of conditions in an 'if' clause and one side of the conditional expression is the same in each condition, is there any shortcut to avoid writing out every expression in full. For examples, rather than writing
if(($name!='Lisa')&&($name!='Leah')&&($name!='Christine')) ... | 2010-05-15 | 2274 | PHP preg functions - examples and comparision preg_match('%CheckUpdateTime(.*?)Main content end%s',$stuff,$gotten);
preg_match_all('%(<td class="destination">.*?)</tr>%s',$gotten[1],$service);
foreach ($service[1] as $train) {
$teapot = preg_split('%</td>%',$train);
foreach ($teapot as $element) {
$el ... | 2009-07-10 | 2215 | If nothing, make it nothing. Here's a peculiar PHP statement I wrote this morning:
if ($aindex[$chk] == "") $aindex[$chk] = "";
What was I doing? I am actually checking to see whether there's anything in an array element and if there isn't (which could be because it's empty or because it doesn't yet exist), then it ... | 2009-06-03 | 1116 | PHP adding arrays / summing arrays A question from my mailbox, answered in relation to our hotel. "I have a number of arrays in PHP and I want to add the elements of the arrays together". "There doesn't appear to be an array_sum function".
So in other words, my correspondent may have room occupancy data such as:
$bed1 = array(2,2,2,0,2,0,0);
$bed2 ... | 2009-05-04 | 1614 | When an array is not an array An array, I was taught, is a sequential series of memory locations in which values of some type are stored. In an array, each of the memory locations is the same size (number of bytes). And so, as I was taught, arrays can be used very efficiently using pointer arithmetic, BUT they need to:
a) Be defined ... | 2008-04-19 | 832 | Displaying data at 5 items per line on a web page If you want to display a list of items in (say) 5 columns on a web page, use a table. A loop in PHP will let you generate all the table cells very easily, then add in a little extra code to deal with:
a) The very first cell (you need to open table and row)
b) Every fifth cell (you need ... | 2006-08-14 | 773 | Breaking bread How can I take ONE thing, discard some of it, and end up with MANY things all of the same type as the original? Surely that doesn't make sense does it?
I could take a packet of half a dozen rolls from the supermarket, made (as they often are in the UK) as a single piece of bread with narrow "tear" ... | 2006-06-22 | 409 | Functions and commands with dangerous names There are some words that we use in our day to day programming life that seem a little scarey and inappropriate to their real task ... and sometimes those words worry trainees on our courses. Examples:
PHP's reset function. It's name seems to threaten to clear our data from somewhere, but all it does ... | 2006-06-05 |
Examples from our training material
5pl.php | Display a list in columns | a1.php | Setting up an array, looking at individual elements | a2.php | Array - dealing with missing elements and finding keys | a3.php | Array_walk example. Also the difference between null and empty | arrarr.php | Summing arrays in PHP | arrx.php | Ways of passing through an array | assoc.php | Associative array - sorting and traversing | fullmenu.php | Ordering system from menu file | herbert.php | finding a value from a list of ranges | menu.txt | Menu for fullmenu.php example | nb.html | Speed networking - generate a page for an attendee | nbm.html | Speed networking - seating matrix generator | patrick.php | User defined sort | pflog.php | Most viewed pictures (Mk 1) | pflog2.php | Where are scripts loaded from (2) | rubys.php | read a file into an array and display selected items | simgrep.php | read a file into an array and display selected items | skills.php | A Small search tool | ts.php | Lengthen or shorten an array | walter.php | regular and associative arrays |
Background information
Some modules are available for download as a sample of our material or under an Open Training Notes License for free download from [here].
Topics covered in this module
Array manipulation. Array functionality. Creating Arrays. Manipulating arrays.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule page.
Well House Consultants specialise in training courses in
Ruby,
Lua,
Python,
Perl,
PHP, and
MySQL. We run
Private Courses throughout the UK (and beyond for longer courses), and
Public Courses at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us.
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.
|