Arrays
I believe that the best way to learn something is to do it. There are several different arrays in the following paragraphs. I want you to look over them try to understand how they work. Then at the bottom of this lesson you can download a copy of this page to run on your own server. Try changing the arrays and making up new ones. I guarantee you will learn arrays very fast!
If you ever have any questions please feel free to post them in the
forums.
[php] <?php
$numbers = array(1,2,3,4,5);
print_r($numbers);
?>[/php]
In this array we make an array named
$numbers and put the following values in it: 1, 2, 3, 4, and 5,
[php] <?php
$labels = range(5, 10);
print_r($labels);
?>[/php]
In this array we make an array named
$labels and put all of the values between 5 and 10 in it.
[php] <?php
$people = array("joe", "bob", "sarah",);
print_r($people);
?>[/php]
First we define an array and call it
$people, then we put "joe", "bob", and "sarah" in it.
Number "1" would print out "bob":
Code:
echo $people[1];
[php] <?php
echo $people[1];
?>[/php]
(Remember that arrays start with ZERO as the first value.)
[php] <?php
$fruit = array("apple", "banana", "watermelon");
print_r($fruit);
?>[/php]
Here we make an array named
$fruit AND:
put "apple" in the first array value
put "banana" in the second array value
put "watermelon" in the third array value
[php] <?php
$fruit = explode(",", "apple,banana,watermelon");
print_r($fruit);
?>[/php]
Here we take a sentence (apple,banana,watermelon) and "explode" it.
In other words we split it by the character "
,". So everywhere explode
finds a "
," it will split the string. Then the three pieces are put into
an array named
$fruit. In the next lesson we will talk more about
explode.
[php] <?php
$fruit_weight = array("apple" => 35, "banana" => 29, "watermelon" => 25);
print_r($fruit_weight);
?>[/php]
Here an array named
$fruit_weight is created. Then the array's variables are named something specific like "apple" and given a value. This is important to note - you don't have to except the default 0,1,2,3,4,etc.. array variables: YOU CAN NAME THEM YOURSELF!
(No more
$array[2] now you can name something
$array[yourname].
[php] <?php
$friends = array("John" => 'Happy', "Sue" => 'Fun', "Sam" => "Sad");
print_r($friends);
?>[/php]
Here is an array where you make all of the variables your friends names and then make their values their moods. Notice that for the last intry ("Sam" => "Sad")I put double quotaition marks insted of single quotaition marks on the value. Either way it dosen't make any difference.
Now lets print each value:
[php] <?php
echo $friends['John']. "";
echo $friends[Sue]. "";
echo $friends["Sam"]. "";
?>[/php]
Note that in the above code you can use double quotaition marks, single quotaition marks, or NO quotaition marks when printing an array's value.
[php] <?php
$labels = range(5, 10);
for($i = 5; $i <= 10; $i++)
{
$labels[] = $i;
}
print_r($labels);
?>[/php]
Make all the array
$labels contain all of the values between '5' and '10'.
Then add all of the values of
$i to the array,
starting at '5' and ending when
$i is equal to '10'.
[php] <?php
$labels = range(1, 6);
for($i = 0; $i <= 5; $i++)
{
$labels[] = $i;
}
print_r($labels);
?>[/php]
Make all the array
$labels contain all of the values between '1' and '6'.
Then add all of the values of
$i to the array,
starting at '0' and ending when
$i is equal to '5'.
You can
download the php code here.