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.
$numbers = array(1,2,3,4,5);
print_r($numbers);
?>
In this array we make an array named $numbers and put the following values in it: 1, 2, 3, 4, and 5,
$labels = range(5, 10);
print_r($labels);
?>
In this array we make an array named $labels and put all of the values between 5 and 10 in it.
$people = array("joe", "bob", "sarah",);
print_r($people);
?>
First we define an array and call it $people, then we put "joe", "bob", and "sarah" in it.
Number "1" would print out "bob":
echo $people[1];
echo $people[1];
?>
(Remember that arrays start with ZERO as the first value.)
$fruit = array("apple", "banana", "watermelon");
print_r($fruit);
?>
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
$fruit = explode(",", "apple,banana,watermelon");
print_r($fruit);
?>
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.
$fruit_weight = array("apple" => 35, "banana" => 29, "watermelon" => 25);
print_r($fruit_weight);
?>
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].
$friends = array("John" => 'Happy', "Sue" => 'Fun', "Sam" => "Sad");
print_r($friends);
?>
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:
echo $friends['John']. "";
echo $friends[Sue]. "";
echo $friends["Sam"]. "";
?>
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.
$labels = range(5, 10);
for($i = 5; $i <= 10; $i++)
{
$labels[] = $i;
}
print_r($labels);
?>
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'.
$labels = range(1, 6);
for($i = 0; $i <= 5; $i++)
{
$labels[] = $i;
}
print_r($labels);
?>
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.