Logo         Community
The Company
Your Account
Contact Us
Array Manipulation With PHP
Take an in-depth look at PHP's array manipulation tools for faster, more efficient code.

| 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

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '

I would use the notation



''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '

would be


- 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

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '

I would use the key "new hope"

$starwars["new hope"]

while the value

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '

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

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback