Array Manipulation With PHP4 (
Page 1 of 8 )
This may come as a bit of a shock to you, but PHP's array
functions can do a lot more than just count the elements of an array or
iterate through key-value pairs. This article takes an in-depth look at
PHP's less well-known array manipulation tools, illustrating how they can
be used to write tighter, more efficient code...and have some fun in the
bargain as well!As a PHP developer myself, I spend a fair part of my time looking for ways to
write cleaner, more efficient code. I'll be the first to admit that I don't
succeed too often; however, that doesn't stop me from making the effort. And
nine times out of ten, I don't have to look too far - the PHP community is
incredibly generous with its advice and assistance, and the language itself
offers tools which can slice through complex problems in much the same way as a
hot knife slices through butter.
Nowhere is this clearer than in the case
of PHP's built-in functions, which offer developers power and flexibility of the
sort not seen in most other languages. Sadly, most developers don't use this
power - not because they don't want to, but simply because they're not even
aware of the full extent of PHP's capabilities. And this lack of knowledge can
often spell the difference between a tight, efficient Web application and a
slow, inefficient one.
Over the next few pages, I'm going to illustrate
my point by taking an in-depth look at PHP's array manipulation capabilities. If
you're like most developers, you probably use arrays extensively in your
development activities; however, your knowledge of array manipulation techniques
is limited to counting the elements of an array or iterating through key-value
pairs.
PHP's array manipulation functions allow you to do much, much
more...and this article will open your eyes to the possibilities.
In
addition to providing a gentle introduction to PHP programming in general (and
PHP array manipulation in particular), this article will offer you a broad
overview of PHP's array functions, serving as both a handy reference and a tool
to help you write more efficient code. Regardless of whether you're new to PHP
or if you've been working with the language for a while, you should find
something interesting in here.
Let's get started!{mospagebreak title=Just
Friends} We'll begin right at the top, with some very basic definitions and
concepts.
Unlike string and numeric variables, which typically hold a
single value, an array can best be thought of as a "container" variable, which
can contain one or more values. For example,
<?
$friends = array("Rachel", "Monica", "Phoebe", "Joey", "Chandler", "Ross");
?>
Here, $friends is an array variable, which contains the
values "Rachel", "Monica", "Phoebe", "Joey", "Chandler", and
"Ross".
Array variables are particularly useful for grouping related
values together - names, dates, phone numbers of ex-girlfriends et
al.
The various elements of the array are accessed via an index number,
with the first element starting at zero. So, to access the element
"Rachel"
I would use the notation
$friends[0]
while
"Joey"
would be
$friends[3]
- essentially, the array variable name followed by the index
number enclosed within square braces. Geeks refer to this as "zero-based
indexing".
PHP also allows you to replace indices with user-defined
"keys", in order to create a slightly different type of array. Each key is
unique, and corresponds to a single value within the array.
<?
$starwars = array("new hope" => "Luke", "teacher" => "Yoda", "bad guy" =>
"Darth");
?>
In this case, $starwars is an array variable containing three
key-value pairs. The => symbol is used to indicate the association between a
key and its value.
In order to access the value
"Luke"
I would use the key "new hope"
$starwars["new hope"]
while the value
"Darth"
would be accessible via the key "bad guy"
$starwars["bad guy"]
This type of array is sometimes referred to as a "hash" or
"hash table" - if you've ever used Perl, you'll see the similarities to the Perl
hash variable.
PHP arrays can store strings, number and other arrays. In
fact, the same array can store multiple types of data, a luxury not available in
many other programming languages. Here's an example:
<?
// create array
$all_mixed_up = array(
"numbers" => array("one", 2, 3, "four"),
"superheroes" => array("spiderman", "superman", "captain marvel"),
"icecream" => array("chocolate" => "brown", "vanilla" => "white",
"strawberry" => "pink"),
);
// returns null - no element with key 2
echo $all_mixed_up[2];
// returns "Array"
echo $all_mixed_up["numbers"];
// returns "one"
echo $all_mixed_up["numbers"][0];
// returns "captain marvel"
echo $all_mixed_up["superheroes"][2];
// returns "white"
echo $all_mixed_up["icecream"]["vanilla"];
?>