Introduction to PHP and MySQL Kirkwood Center for Continuing Education By Fred McClurg, frmcclurg@gmail.com © Copyright 2010 All Rights Reserved. 1 Chapter Six Associative Arrays http://webcert.kirkwood.edu/~fmcclurg/co urses/php/slides/chapter06.associative.ppt 2 Associative Array (aka Hash) Description: An associative array uses an unique string called a “key” as the index to each element. Syntax: $var['key'] = $value; 3 Associative Array Element Initialization Initialization via multiple statements: <?php $month['jan'] $month['feb'] $month['mar'] $month['apr'] ... = = = = 31; 28; 31; 30; foreach ( $month as $name => $days ) { printf( "%s has %d days<br />", $name, $days ); } ?> Note: The internal order of an associative array can not be guaranteed. 4 Hash Array Initialization Construct Hash array initialization via a single statement: <?php $user = array( 'mluther' 'bgraham' 'dlmoody' 'jwesley' => => => => 'Martin', 'Billy', 'D.L.', 'John' ); foreach ( $user as $uname => $fname ) { printf( "Username: %s &nbsp; First: %s<br />", $uname, $fname ); } ?> Note: The internal order of an associative array can not be guaranteed. 5 Chapter Six Looping Associative Arrays 6 Looping Hash Arrays via “foreach” Description: The “foreach” construct can also be used to iterate through an associative array to obtain the key and value of each element. Preferred Usage: 1. When the element value and the element key is needed. 2. When an operation is performed on every element. 7 The “foreach” Hash Array Syntax Syntax: foreach ( $array as $key => $value ) { statement; ... } 8 A “foreach” Hash Array Example Example: <?php $color = array( "red" => "#FF0000", "green" => "#00FF00", "blue" => "#0000FF" ); foreach ( $color as $key => $val ) { printf( "\$color['%s']: %s<br />", $key, $val ); } ?> 9 Student Exercise 6.2 Using a Hash Problem: Write a PHP program that creates an associative array with the zip code as the key and the city name as the value. Requirements: 1. In one statement, initialize an associative array to the zip code and city name. (ex: KW = 52404, NL = 52317, CV = 52241) 2. Use a foreach operator to print the zip code and the associated city name. 10 Student Solution 6.2 Using a Hash <?php $hash = array( '52233' '52302' '52314' '52328' '52333' => => => => => 'Hiawatha', 'Marion', 'Mt Vernon', 'Robins', 'Solon' ); foreach ( $hash as $zip => $city ) { printf( "City: %s &nbsp; Zip: %s<br />", $city, $zip ); } ?> 11 Using a Variable as Hash Key <?php $year = 1960; // leap year $thirty = array( 'sep', 'apr', 'jun', 'nov' ); $thirtyOne = array( 'jan', 'mar', 'may', 'jul', 'aug', 'oct', 'dec' ); foreach( $thirty as $name ) $month[$name] = 30; // initialize 30 day months foreach( $thirtyOne as $name ) $month[$name] = 31; // initialize 31 day months // $month['feb'] = isLeapYear( $year ) ? 29 : 28; $month['feb'] = 29; // initialize leap year month // how would you print out months in order? foreach ( $month as $name => $days ) { printf( "%s has %d days<br />", $name, $days ); } ?> 12 Using a Hash to Create an Unique List Description: An associative array can be used to reduce a list to unique values. <?php $cartoon = array( 'Fred', 'Barney', 'Fred', 'Wilma', 'Fred', 'Pebbles', 'Fred', 'Dino' ); foreach ( $cartoon as $name ) $unique[$name]++; // count occurrences foreach ( $unique as $name => $occur ) printf( "Actor %s occurred %d times<br />", $name, $occur ); ?> Note: All uninitialized variables have a value of NULL that are promoted to zero 13 before being incremented. Looping Hashes via “each” in a “while” Example: <?php $color = array( "red" => "#FF0000", "green" => "#00FF00", "blue" => "#0000FF" ); while (list($key, $val) = each($color)) { printf( "\$color['%s']: %s<br />", $key, $val ); } ?> 14 to be continued ... http://webcert.kirkwood.edu/~fmcclurg/cour ses/php/slides/chapter06c.multidimential.ppt