logo
Published on Code2Design (http://www.code2design.com)

Lesson 14 - Arrays

By David
Created Nov 10 2006 - 7:47pm

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 [1].

<?php
$numbers 
= array(1,2,3,4,5);
?>

In this array we make an array named $numbers and put the following values in it: 1, 2, 3, 4, and 5,

<?php
$labels 
range(510);
?>

In this array we make an array named $labels and put all of the values between 5 and 10 in it.

<?php
$people 
= array("joe""bob""sarah",);
?>

Here we define an array and call it $people, then we put "joe", "bob", and "sarah" in it - but how do we print them?! You might have tried "print $people;" by now, but that just wont work. You see you can't echo or print arrays like a normal variable because PHP can't tell which value in the array you want to print. So you have two choices - you can either print out the whole array with "print_r()", or you can use the array "key" to print out a certain value.

<?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"]. "";
?>

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
$labels 
range(510);
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(16);
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 [2] for a list of all the functions you can use with arrays.

AttachmentSize
C2D_lesson_14.zip [3]1.74 KB

Source URL:
http://www.code2design.com/lesson_14_arrays