This time around, however, I have personal investment: I'm using WordPress to keep track of all my webcomics, and I want to improve on WordPress' default capabilities. But before I can do that, I have to understand arrays. So I'd like to the opportunity to describe what I *think* I understand, in the hopes that you lot can point out what I'm getting terribly, terribly wrong. :)
So:
An array is essentially a list of information grouped together. For example, a single comic in my WordPress site might list
- the comic name
- the name of the png to display
- a brief teaser that would be inserted into an rss feed in place of the png, to entice subscribers to come to my site (note: I haven't been able to get that feature to work, sigh)
If I were to take the information from a single comic and express it as an array, it might look something like this:
$comic = array (
"title" => "A Brief Tactical Assessment",
"image" => "hd20060804.png",
"teaser" => "Alex, Monk and Mark discuss the merits of Viktor's plan."
);
I could also enter in each piece of the array individually:
$comic['title'] => "A Brief Tactical Assessment"
$comic['image'] => "hd20060804.png"
$comic['teaser'] => "Alex, Monk and Mark discuss the merits of Viktor's plan."
$comic is the name of the array. The information stored in each iteration of the array is called a value. The label in the brackets is called a key, and it is used to keep track of all the values in stored in the array -- in my example the three keys actually describe the values in the array, but that isn't necessary. When no key in the brackets is specified, it first key defaults to 0 and each additional key increments by 1 every time a new value is added.
So...
$comic[] => "A Brief Tactical Assessment"
$comic[] => "hd20060804.png"
$comic[] => "Alex, Monk and Mark discuss the merits of Viktor's plan."
will create keys 0, 1, and 2
0 is for the value "A Brief Tactical Assessment"
1 is for the value "hd20060804.png"
2 is for the value "Alex, Monk and Mark discuss the merits of Viktor's plan."
That is a single array. As it stands, a single array can't keep track of ten years of help desk archives. Each individual comic, if it's set up in the example I gave above, would use the same keys (title, image, teaser) but would have different values for each, and every time an existing key is given a new value, it overwrites the original value.
In order to use the same keys for each comic while keeping all the information separate, I'll have to create a multidimensional array. A multidimensional array is basically an array with multiple keys. For example:
$comic['comic1']['title'] => "Never Give Them Your Real Name"
$comic['comic1']['image'] => "hd20060802.png"
$comic['comic1']['teaser'] => "Viktor makes a command decision."
$comic['comic2']['title'] => "A Plan, After a Fashion"
$comic['comic2']['image'] => "hd20060803.png"
$comic['comic2']['teaser'] => "Viktor claims to have a plan, but the details need work."
$comic['comic3']['title'] => "A Brief Tactical Assessment"
$comic['comic3']['image'] => "hd20060804.png"
$comic['comic3']['teaser'] => "Alex, Monk and Mark discuss the merits of Viktor's plan."
This array now has a value that groups other values together -- each value ['comicn'] has its own values for the comic title, comic image, and comic teaser, and they can be stored without them overwriting each other.
Of course, typing in a value for each comic would be more than a little tedious, since I have more than two thousand comics in my archives... but I suspect at some point in the future I'll learn how to use variable and constants as keys in an array, which means that instead of naming "master" key on my own, I could do this:
$comic[$comictype;][$pubdate;]['title'] = "Never Give Them Your Real Name."
etc.
Where $comictype; would be the name of the webcomic instead of the individual comic name (i.e., Help Desk, Kernel Panic, Old Skool Webcomic) and $pubdate; would be the date the comic was published (assuming I publish one comic a day, which is actually an overly ambitious assumption given my general lack of discipline).
Note that I don't pretend that this is the real way to use a variable as a key -- I haven't reached that lesson yet. That's just me trying to describe it.
Anyway, this would allow me not only to keep track of each individual comic values by separating the comics by date of publication, but it would allow me to manage multiple comics in exactly the same way, by inserting a higher level value that is based on the name of each comic. But that kind of stuff is more the mechanics of php, and at this point I'm trying to make sure I understand the concept of arrays.
I think I've got it... at least, the basics. There's other stuff in there I need to look at more closely... exploding arrays, imploding arrays, combining arrays and splitting arrays apart... but I don't want to learn the fancy stuff until I have the idea behind an array's basic use down.
Am I on the right track here?