Array Manipulation With PHP4 - Having Your Cake (
Page 2 of 8 )
As you may have already
guessed, defining an array variable takes place via the array() function -
here's how:
<?
$desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate
fudge cake");
?>
The rules for choosing an array variable name are the same as
those for any other PHP variable - it must begin with a letter, and can
optionally be followed by more letters and numbers.
Alternatively, you
can define an array by specifying values for each element in the index notation,
like this:
<?
$desserts[0] = "chocolate mousse";
$desserts[1] = "tiramisu";
$desserts[2] = "apple pie";
$desserts[3] = "chocolate fudge cake";
?>
Incidentally, you can omit the index numbers if you prefer -
the following snippet is equivalent to the one above:
<?
$desserts[] = "chocolate mousse";
$desserts[] = "tiramisu";
$desserts[] = "apple pie";
$desserts[] = "chocolate fudge cake";
?>
If you'd prefer to use keys instead of numeric indices, the
following examples should make you happier:
<?
$movies = array("romance" => "Moulin Rouge", "epic" => "Gladiator",
"action" => "The Terminator");
// this is equivalent
$movies["romance"] = "Moulin Rouge";
$movies["epic"] = "Gladiator";
$movies["action"] = "The Terminator";
?>
You can use the range() function to automatically create an
array containing a range of elements:
<?
// returns the array ("30", "31", "32", "33", "34", "35", "36", "37", "38",
"39", "40")
$thirties = range(30, 40);
// returns the array ("i", "j", "k", "l", "m", "n", "o")
$alphabet = range("i", "o");
?>
You can add elements to the array in a similar manner - for
example, if you wanted to add the element "apricot fritters" to the $desserts
array, you would use this:
<?
$desserts[4] = "apricot fritters";
?>
and the array would now look like this:
<?
$desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate
fudge cake", "apricot fritters");
?>
The same goes for non-numeric keys - adding a new key to the
$movies array
<?
$movies["horror"] = "The Sixth Sense";
?>
alters it to read:
<?
$movies = array("romance" => "Moulin Rouge", "epic" => "Gladiator",
"action" => "The Terminator", "horror" => "The Sixth Sense");
?>
In order to modify an element of an array, you would simply
assign a new value to the corresponding scalar variable. If you wanted to
replace "chocolate fudge cake" with "chocolate chip cookies", you'd simply use
<?
$desserts[3] = "chocolate chip cookies";
?>
and the array would now read
<?
$desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate
chip cookies", "apricot fritters");
?>
You can do the same with named keys - the statement
<?
$movies["action"] = "Rambo";
?>
further alters the $movies array to
<?
$movies = array("romance" => "Moulin Rouge", "epic" => "Gladiator",
"action" => "Rambo", "horror" => "The Sixth Sense");
?>
If it walks like an array, talks like an array and looks like
an array, it must be an array...right? Well, you can always verify your
suspicions with the is_array() function, which comes in handy if you need to
test whether a particular variable is an array or not.
<?
// create array
$desserts = array("chocolate mousse", "tiramisu", "apple pie", "chocolate
fudge cake", "apricot fritters");
// returns true
echo is_array($desserts);
$desserts = "blueberry ice cream";
// returns false
echo is_array($desserts);
?>