• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 11. Arrays > Associative Arrays

Associative Arrays

To appreciate associative arrays, you must fully understand the value of arrays generally. You’ve learned in this chapter that arrays have two general benefits: You can store multiple pieces of data in a single structured variable, and you can perform interesting operations on the contents of arrays. Unlike arrays, which contain multiple single-item values in their indexes, associative arrays have pairs of values. That is, a regular array could contain three items (for example, “Phillip,”“Kerman,” and 37), but an associative array could hold three pairs (say, firstname: “Phillip,” lastname: “Kerman,” and age: 37). The value for the item in the zero index is "Phillip" in either case, but items in any index of an associative array are referred to by their name (also called property)—in this case firstname. It doesn’t matter in which slot the pair firstname:"Phillip" resides, you can always find it by referring to firstname.

Although associative arrays make your structured data vastly easier to manage (because you’ll never need to remember in which index a particular item of data resides), they have one drawback: Associative arrays cannot use the Array object’s methods or properties. That includes the sort() method (which really isn’t interesting since position is irrelevant) and the length property (which would be nice). You can, however, loop through an associative array by using the for in loop.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint