O'Reilly logo

Core PHP Programming, Third Edition by Leon Atkinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5.2. Indexing Arrays

So far we've only seen arrays indexed by integers, but it is also permissible to use strings. Sometimes these are called associative arrays, or hashes. They are helpful in situations where you are collecting different types of information into one array. You could build into your code a system where element zero is a name, element one is a location, and element two is an occupation. Listing 5.3 is a more elegant way to accomplish this.

Listing 5.3. Indexing arrays with strings
 <?php // fill in some information $UserInfo["Name"] = "Leon Atkinson"; $UserInfo["Location"] = "Martinez, California"; $UserInfo["Occupation"] = "Programmer"; //loop over values foreach($UserInfo as $key=>$value) { print("$key is $value.<br>\n"); } ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required