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

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

new
Creativity
Family Travel
Southwest USA
Irish Culture
Home Finance
Comedy Movies
Romance Novels


dailyclick
All times in EST

Full Schedule
g
g PHP Site

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



This site needs an editor - click to learn more!

Add The+PHP+Associative+Array to Twitter Add The+PHP+Associative+Array to Facebook Add The+PHP+Associative+Array to MySpace Add The+PHP+Associative+Array to Del.icio.us Digg The+PHP+Associative+Array Add The+PHP+Associative+Array to Yahoo My Web Add The+PHP+Associative+Array to Google Bookmarks Add The+PHP+Associative+Array to Stumbleupon Add The+PHP+Associative+Array to Reddit




RSS | Editor's Picks Articles | Top Ten Articles | Previous Features | Site Map


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


Content copyright © 2014 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 BellaOnline Administration for details.

g


g features
Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


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


BellaOnline Editor