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
$numbers = array(1,2,3,4,5);
?><?php
$labels = range(5, 10);
?><?php
$people = array("joe", "bob", "sarah",);
?><?php
print_r($people);
//print the whole formated array.
?>Or we can use the key of each value we want printed:
Number "1" would print out "bob":
<?php
echo $people[1];
?>Number "0" would print out "joe":
<?php
echo $people[0];
?>Number "2" would print out "sarah":
<?php
echo $people[2];
?>Remember that arrays start with ZERO as the first "Key". So if you have an array with 10 values in it that array will start at "0" and go to "9" as the last "Key".
<?php
$fruit = array("apple", "banana", "watermelon");
?>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
$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.
<?php
$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].
<?php
$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:
<?php
echo $friends['John']. "";
echo $friends[Sue]. "";
echo $friends["Sam"]. "";
?><?php
$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'.
<?php
$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'.
Visit the PHP homepage of arrays at http://www.php.net/array for a list of all the functions you can use with arrays.
| Attachment | Size |
|---|---|
| C2D_lesson_14.zip | 1.74 KB |