PHP syntax: create an associative array

Posted December 28, 2003 by Quinn McHenry in PHP programming

An associative array can use non-numeric index values and are useful because they can be iterated over sequentially and values can be extracted individually by index. Database select results can be returned as associative arrays in which the column names become indicies.


The following code creates an associative array of the first few US states and their capitals. The state name is the index and the capital is the value.

$capitals=array("Alabama"=>"Montgomery",
"Alaska"=>"Juneau",
"Arizona"=>"Phoenix",
"Arkansas"=>"Little Rock",
"California"=>"Sacramento",
"Colorado"=>"Denver");

 

About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.