This may come as a bit of a shock to you, but PHP's arrayfunctions can do a lot more than just count the elements of an array oriterate through key-value pairs. This article takes an in-depth look atPHP's less well-known array manipulation tools, illustrating how they canbe used to write tighter, more efficient code...and have some fun in thebargain as well!
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:
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:
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
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.