IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Learning PHP: Comprehending Arrays
The last time I tried to learn about arrays, I was trying to teach myself C. I was doing *great* going through that book until I hit the chapter on arrays, and the entire thing collapsed at that point. The problem was, at that point, I didn't have any context to put it in. The book (which was, as I recall, a very well-written book) tried to give me one to use -- a database that keeps track of prices at a grocery store -- but because I didn't have any particular investment in that database, I had a really hard time processing the information.

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?
"We are all born originals -- why is it so many of us die copies?"
- Edward Young
New Re: Learning PHP: Comprehending Arrays
A multi-dimensional array is sort of like a matrix in that it has rows and columns. You are getting close to how a database or spreadsheet works. Maybe later you can store the data in a MySQL table and read it and parse some HTML with the comic info in it. First things first though, figure out how arrays work.

Here is a simple explination of arrays:
[link|http://www.tizag.com/phpT/arrays.php|http://www.tizag.com/phpT/arrays.php]

Here is a section of the PHP manual on arrays:
[link|http://www.php.net/manual/en/language.types.array.php|http://www.php.net/m...e.types.array.php]

Keep in mind that the first item in an array can be referenced by the Integer index key 1, if you do not specify an index key it will be 0.

My PHP is rusty and I am trying to learn it myself, so I hope I get this right.

$comic[1]['title'] => "Never Give Them Your Real Name"
$comic[1]['image'] => "hd20060802.png"
$comic[1]['teaser'] => "Viktor makes a command decision."
$comic[2]['title'] => "A Plan, After a Fashion"
$comic[2]['image'] => "hd20060803.png"
$comic[2]['teaser'] => "Viktor claims to have a plan, but the details need work."
$comic[3]['title'] => "A Brief Tactical Assessment"
$comic[3]['image'] => "hd20060804.png"
$comic[3]['teaser'] => "Alex, Monk and Mark discuss the merits of Viktor's plan."

I replaced the comic1 etc with Integers. This is so that it can be referenced with a loop later on using an integer as a variable.

You might want to research the foreach statement:
[link|http://www.php.net/manual/en/control-structures.foreach.php|http://www.php.net/m...tures.foreach.php]

You might be able to loop through the array using foreach and print out each issue of your comic that way without using integers.

Using an Integer as a control you could loop through arrays like so:
[link|http://www.freewebmasterhelp.com/tutorials/php/4|http://www.freewebma...m/tutorials/php/4]

$number = 3;
$x = 0;
while ($x < $number) {
$comic = $x + 1;
echo "Comic $namenumber title is $comic[$x][title]
";
echo "Comic $namenumber image is $comic[$x][image]
";
echo "Comic $namenumber teaser is $comic[$x][teaser]
";
++$x;
}

I am not 100% sure of the img tag parsing, it has been a while since I did that. The forum here does not like the code I used to give an example of making one work in PHP so I removed it.

$number is the number of comics you entered.

$x is the control array that brings them up via the order you entered them.

The "++" is like you learned in C, it adds one to the value in $x before it is read.

I could research it for you more, but duty calls at home. I really want to learn these things as well, and I hope I pointed you in the right direction.




"It is of interest to note that while some dolphins are reported to have learned English -- up to fifty words used in correct context -- no human being has been reported to have learned dolphinese."
Carl Sagan (1934 - 1996)




[link|http://district268.xormad.com|I am from District 268].
New Looks like you have it
Looks like you have a good grasp of how arrays work in PHP.
$comic[$comictype;][$pubdate;]['title'] = "Never Give Them Your Real Name." etc.

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.

That is actually the right way, minus the extra ";" characters you have in there.

The only thing I really have to add has nothing to do with arrays. In PHP you should get in the habit of using ' rather then " unless you need double quotes. It takes a small but not totally ignorable amount of extra procssing power to handle double quotes, becaue there are more things PHP has to scan for in the string.

Jay

I could also say something about PHP arrays actually being maps, since arrays by definition have only a numeric index. But that is a point so pedantic that even among programming geeks few care.
New Arrays and Dictionaries
An array is just a collection, with the usual stipulation that you can access any element by a numeric index. On the other hand, a dictionary (also known as a hash) is a lookup table that has a key to a data value. Some languages emulate an array by means of a dictionary, where a numeric value can be used as a key. Given that your examples use non-numeric indexes, I'd be inclined to say that it more resembles a dictionary, rather than a classic array (at least in the contiguous memory model of C).
New And you're the pedant Jay warned about :-)
Yes, Chris (the first Chris) you've got it exactly right. And once you get to the parts of PHP dealing with database access, you'll see that the functions already map columns to array indices pretty well. Before you get there, let me recommend using one of the DB layers like like [link|http://pear.php.net/manual/en/package.database.db.php|PEAR:DB]. You don't want to worry about MySQL vs. Postgres vs. Oracle issues any more than you have to.
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New Yes, that's largely correct.
Arrays in PHP are farily unique in that most languages at that level offer lists and tables (or dictionaries) as separate structures. PHP puts them together in the one structure, with some slightly odd behaviours with keys as a result.

Arrays are very powerful in PHP, even with a slightly clunky literal notation. You've been exploring using them as a large structure, but they can do a lot of very clever things when treated as a small structure, as well (my favourite is using a short array literal to send named parameters to a function). And nesting! Don't be afraid to next arrays: you can win a lot of organisation using nested loops and nested arrays. And you can learn and use all of that without using things like array_merge(), by the way.

Wade.
"Insert crowbar. Apply force."
New Oh yes, do avoid that
The syntax and operation of array_merge and some of the other "magic" utilities for arrays never became natural for me. Sure, I probably re-invented a wheel or two, but at least I understood what it was doing. Which makes me the guy you always read about in TheDailyWTF that re-invented the String class.
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New I had to experiment with them before I understood them.
array_diff I use a little, though carefully. array_merge I don't use - it's a little bit simplistic for they way I often work with arrays that need that operation; the + operator works much better.

OTOH, I use array_filter a bit.

Wade.
"Insert crowbar. Apply force."
     Learning PHP: Comprehending Arrays - (cwbrenn) - (7)
         Re: Learning PHP: Comprehending Arrays - (orion)
         Looks like you have it - (JayMehaffey)
         Arrays and Dictionaries - (ChrisR) - (1)
             And you're the pedant Jay warned about :-) - (drewk)
         Yes, that's largely correct. - (static) - (2)
             Oh yes, do avoid that - (drewk) - (1)
                 I had to experiment with them before I understood them. - (static)

Punk rock.
67 ms