|
Arrays are a
collections of data, grouped into one variable, along a variety of streams. For
example, I might have 6 names in one array, and to access the names I'll use
$names[0], $names[1], etc. This function is often used with the
FOR and FOREACH
functions, but as this tutorial is written for beginners, I'll stick to using
the WHILE() statement instead.
A Simple Array
For our first example, lets imagine that we have a class of 10 students at a school,
and need to put all of their names into an array. In our code below we will
input all their names sequentially:
<?
$class = "Hellview School, Class 10SATAN 2004.";
$names = array (
"anna",
"emma",
"robbie",
"evan",
"lewis",
"simon",
"nicole",
"toby",
"caroline",
"satan"
);
?> |
We now have all of the names of the pupils
in our class in an array called $names. If we want to output these values
sequentially, all we need to do is PRINT the variable out as follows:
This is, of course, very exciting,
but what if we want to print out the first and last names in the array
only? Well, the beauty of arrays is that you can do this
with absolute ease, as this snippet of code reveals:
<?
echo 'First name: ' . $names[0] . '<br>Last
name: ' . $names[9];
?>
|
Note: Remember that with arrays,
the first entry isn't 1... its 0! So if we want to output the first
value we will specify $names[0].
Advanced Array Instructions
Ok, that was fairly simple. Now I’ll show you how to loop through each stream,
outputting each value as you go (but, unlike simply PRINTing it, this method
will let you process the results a little). Our first two variables in the
example below tell the WHILE loop where to start and finish. The $finish
variable counts how many streams there are in the array.
<?
$start = 0;
$finish = count($names);
while ( $start <= $finish )
{
echo 'Position :' . $start . ' has the name <b>' . $names[$start] .
'</b> in it.<br>';
$start = $start + 1;
}
echo 'Finished outputting names for the class: ' . $class;
?> |
Another important function when using
arrays, especially for dynamically created arrays, is EXPLODE(). This
function will take a separator that you define, and go through a string,
dividing it into parts appropriately. For example, if I had a simple string
variable that had “anna,emma,robbie,evan” in it, I could use EXPLODE inside
a loop control structure, to output these values into an array. This is the
basis of how dynamic arrays work. Here's an example of the EXPLODE() function at
work:
<?php
$string = "anna,emma,robbie,evan";
$exploded = explode (",", $string);
echo '$string is now an array.<br>';
$start = 0;
$finish = count($exploded);
while ( $start < $finish )
{
echo 'Position :' . $start . ' has the name <b>' . $exploded[$start]
. '</b> in it.<br>';
$start = $start + 1;
}
echo 'Finished.';
?>
|
So, as you can see, you can use anything as a
separator, whether it be a comma, or small unique string, or a space,
anything! It could even be a sentence or a whole story! The
possibilities are endless. I hope this tutorial has helped you
along with arrays and slightly with EXPLODE(). Just keep in mind I only
outlined the explode() function, it wasn’t meant to be an
in-depth
tutorial, and there are additional quirks to it if you want to research
them further. Please don’t hesitate to contact me about this tutorial if you
need to, click on my name below which will refer you to the Creative
Forums, where you can Private Message or Email me. You may be required
to sign up.
- Tutorial written by Scrowler
|