logo
g Text Version
Auto
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Society & Culture
Sports
Travel & Leisure
TV & Movies

dailyclick
Bored? Games!
Postcards
Astrology
Take a Quiz
Rate My Photo

new
Sewing
Jewelry Making
ABC Soaps
Fragrance
Movie Mistakes
Honeymoon
MP3 / iTunes


dailyclick
All times in EST

Low Carb: 8:00 PM

Full Schedule
g
g PHP Site
Lisa Dozois
BellaOnline's PHP Editor

g

The PHP Associative Array
Guest Author - Diane Cipollo

In the previous tutorial, you learned that an array is a variable that stores several values or elements of data organized by index keys.

$books[0] = "Gone With The Wind";
$books[1] = "Little Women";
$books[2] = "War And Peach";

An associative array also uses index keys but these keys are not numeric as in the example above. The keys in an associative array are "named" keys which means that the keys are usually character strings. In the example below, you can see that the keys are words (title, name, organization) that have a meaning for the matching values (Professor, Mike Smith, Ohio State University).

$members = array(
"title" => "Professor",
"name" => "Mike Smith",
"organization" => "OSU"
);

The example above also demonstrates that you use the array function to create or initialize an associative array. The name of the array variable ($members) is followed by an equal sign and then the function name (array). Next you have the key-value pairs. Notice that these pairs are separated by commas. The key is "matched" to its value with the => and the whole group is enclosed in parenthesis. Last you have the semicolon. An associative array works much the same as a regular array.

  • You use the array square brackets to add a key-value pair to the array or to create an array.
    $members['tenured'] = "yes";

  • You use the key to reference an element in an associative array.
    echo "$members['name']";




RSS | Previous Features | Site Map


Content copyright © 2009 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact Lisa Dozois for details.

Digg! g delicious Save to Del.icio.us

g


For FREE email updates, subscribe to the PHP Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor

g features
Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Growing a Garden
Veggies and Flowers
Veggies Only
Flowers Only
No Garden

g


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2009 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor