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.
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.
No Comments for this page.