Saturday, December 21, 2024
HomeWeb application programmingPHP programmingPHP syntax: create an associative array

PHP syntax: create an associative array

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

Quinn McHenry
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.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!