Left Quote    I think there is a world market for maybe five computers.
- Thomas Watson, Chairman of IBM 1943    
Right Quote
[login] | [Register]

Simple PHP Tutorial

by: Jester
Page: 3 of 9
(View All)

Arrays, Multi-Dimensional Arrays

When i first started learning PHP arrays confused me. I learned Perl first; arrays and variables in Perl are represented with different symbols. Not so in PHP, an array looks exactly the same as a variable, it starts with the dollar ($) sign.

$array = array('cat', 'dog', 'fish', 'rabbit');

Think of an array as a variable that can store many separate values. We can access each item in the array.

Now each item of the array is accessed using the variable name, followed directly by square brackets containing the number of the item to be accessed. The number listing of the array starts at zero, so the first item is 0, second is 1, third is 2, etc.


Let's have a look a little deeper into arrays. Arrays have a "key" and a "value". The key is what you use to access the value.

See how each number(key) has an association with it's value? The "keys" don't have to be numbers. If the keys aren't defined, they will default to integers to represent each array item. Let's define another array.

,br> Now we can use the key associated with it's value to return the values:

This works like associative arrays in Perl, the methods are almost identical. Arrays don't stop here, they can become very complex, especially when we get into multi-dimensional arrays.

Multi-dimensional Arrays

We can define more complex arrays like so:

The above is a two-dimensional array, it has two "levels" to it. It has three arrays stored within one array. You can access each array within the array by using the KEY associated with a deeper array. You can then use the keys within the deeper arrays to access the array values.

As you can see, arrays can become very complex, you could define three, four, five-dimensional arrays.

Ok, I think that about does it for arrays, let's go on to conditionals.

1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  
Next »

No Comments for this page.

You Must be logged in or a member to comment.

Tutorial Stats

Tutorial Stats

1 Total Comments
3.25 Rating of 5 (4 Votes)


Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article



· One File Website
· Pagination with PHP
· Building a Comments Script
· Complete Members System
· Variable Scope

"" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank