Archived Boards and Threads... > Parham's PHP Tutorials

PHP Lesson 05 - arrays

(1/5) > >>

As stated earlier, an array is something that can hold several dynamic values.  This is a case when an array would do more justice than a set of variables:

--- Code: ---$person1 = 'Parham';
$person2 = 'Jeff';
$person3 = 'Joseph';
$person4 = 'Unknown';
$person5 = 'David';
$person6 = 'Alex';

--- End code ---

Why should we have to keep track of all these variables when they all hold the same type of information?  An array can be set using the array() function.  Much like a variable, an array name also starts with a dollar sign.  A simple recode of the above will produce this code (and give you an example of how to declare an array):

--- Code: ---$people = array('Parham','Jeff','Joseph','Unknown','David','Alex');

--- End code ---

Notice instead of declaring a new variable for each name, I've created an array called $people (note the plural word also) and put all the names inside that one array.  Here is how PHP interprets the above array:

[0] => 'Parham';
[1] => 'Jeff';
[2] => 'Joseph';
[3] => 'Unknown';
[4] => 'David';
[5] => 'Alex';

The numbers on the left are known as the "keys" of the array and help keep track of the "values" on the right, inside the array.  The "=>" operator is used to associate the "keys" with the "values" inside the array.  Arrays by convention begin with the 0 "key".  If you have 200 values inside an array, the first "key" will be 0 and the last "key" will be 199.

If you're a programmer that has some OOP language background like Java or C or you don't like using the array() function, you might be more comfortable declaring your arrays like this:

--- Code: ---$people[] = 'Parham';
$people[] = 'Jeff';
$people[] = 'Joseph';
$people[] = 'Unknown';
$people[] = 'David';
$people[] = 'Alex';

--- End code ---

Declaring your arrays like this or with the array() function makes absolutely no difference, this is purely a matter of what you're comfortable with.  If you actually look at the above array() function example, you might realize how similar the above and this example actually are.  This method of declaring an array also exists because PHP uses it internally for HTML form parsing (we'll learn about this in a later lesson).

We [now] know that keys start from 0, this is very important.  We'll now talk about how to retrieve particular values inside these arrays.  Again, I'll show by example rather than try to explain in too much detail:

--- Code: ---$people = array('Parham','Jeff','Joseph','Unknown','David','Alex'); //declare the array
print $people[0]; //prints "Parham", the first value in the array
print $people[4]; //prints "David"...
print $people[5]; //prints "Alex"...

--- End code ---

Easy enough?  I want to take a quick sidestep then and give a quick lesson on printing again.  To print "newlines" (when you press "enter" on your keyboard, the cursor goes to a new line and creates a new hidden character called the "newline" character), you have to use "\n" in your strings.  If you wish to use the "\n" character to skip to the next line, you have to use double quotes.  Example:

--- Code: ---print "Parham\nteaches\nPHP"; //remember to use double quotes when using the \n characters

--- End code ---

That will print:

Let's go back to arrays now (sorry about that, the last lesson should have covered the "newline" character as well).  If you want your arrays to be a little more in depth, you can assign your own keys to the array values (This creates associated-arrays because you associate keys with values yourself).  For example:

--- Code: ---$people = array('person1' => 'Parham','person2' => 'Jeff','person3' => 'Joseph','person4' => 'Unknown','person5' => 'David','person6' => 'Alex');

--- End code ---

This would produce:
['person1'] => 'Parham';
['person2'] => 'Jeff';
['person3'] => 'Joseph';
['person4'] => 'Unknown';
['person5'] => 'David';
['person6'] => 'Alex';

REMEMBER that the keys have to be unique; any values that have the same key will be replaced by the most recent value added.  To access specific elements inside our array, we can do this (again by example):

--- Code: ---$people = array('person1' => 'Parham','person2' => 'Jeff','person3' => 'Joseph','person4' => 'Unknown','person5' => 'David','person6' => 'Alex');
print $people['person1']; //prints "Parham"
print $people['person5']; //prints "David";
print $people['person6']; //prints "Alex";

--- End code ---

And that's about it for declaring arrays (sorry about the sidetrack to printing "newlines").

hehe... >:D
oh nevermind hehe.


--- Quote from: Aquilo on August 29, 2003, 07:02:35 PM ---hehe... >:D
oh nevermind hehe.

--- End quote ---


I was going to mess around with folks about associated-arrays and sort()  :D but what I was going to post would have confused folks trying to learn! ;D

[edit] did you get the code I IMed you??

Parham i like these lessons :) i finally get what array is..  ( i had c++ at school.. the book wasn't that good niter was the teacher)


[0] Message Index

[#] Next page

Go to full version