Presentation is loading. Please wait.

Presentation is loading. Please wait.

Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.

Similar presentations


Presentation on theme: "Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1."— Presentation transcript:

1 Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, frmcclurg@gmail.com © Copyright 2010 All Rights Reserved. 1

2 Chapter Six Associative Arrays 2 http://webcert.kirkwood.edu/~fmcclurg/co urses/php/slides/chapter06.associative.ppt

3 Description: An associative array uses an unique string called a “key” as the index to each element. Syntax: $var['key'] = $value; Associative Array (aka Hash) 3

4 Initialization via multiple statements: <?php $month['jan'] = 31; $month['feb'] = 28; $month['mar'] = 31; $month['apr'] = 30;... foreach ( $month as $name => $days ) { printf( "%s has %d days ", $name, $days ); } ?> Note: The internal order of an associative array can not be guaranteed. Associative Array Element Initialization 4

5 Hash array initialization via a single statement: <?php $user = array( 'mluther' => 'Martin', 'bgraham' => 'Billy', 'dlmoody' => 'D.L.', 'jwesley' => 'John' ); foreach ( $user as $uname => $fname ) { printf( "Username: %s First: %s ", $uname, $fname ); } ?> Note: The internal order of an associative array can not be guaranteed. Hash Array Initialization Construct 5

6 Chapter Six Looping Associative Arrays 6

7 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. Looping Hash Arrays via “foreach” 7

8 Syntax: foreach ( $array as $key => $value ) { statement;... } The “foreach” Hash Array Syntax 8

9 Example: <?php $color = array( "red" => "#FF0000", "green" => "#00FF00", "blue" => "#0000FF" ); foreach ( $color as $key => $val ) { printf( "\$color['%s']: %s ", $key, $val ); } ?> A “foreach” Hash Array Example 9

10 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. Student Exercise 6.2 Using a Hash 10

11 <?php $hash = array( '52233' => 'Hiawatha', '52302' => 'Marion', '52314' => 'Mt Vernon', '52328' => 'Robins', '52333' => 'Solon' ); foreach ( $hash as $zip => $city ) { printf( "City: %s Zip: %s ", $city, $zip ); } ?> Student Solution 6.2 Using a Hash 11

12 <?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 ", $name, $days ); } ?> Using a Variable as Hash Key 12

13 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 ", $name, $occur ); ?> Note: All uninitialized variables have a value of NULL that are promoted to zero before being incremented. Using a Hash to Create an Unique List 13

14 Example: <?php $color = array( "red" => "#FF0000", "green" => "#00FF00", "blue" => "#0000FF" ); while (list($key, $val) = each($color)) { printf( "\$color['%s']: %s ", $key, $val ); } ?> Looping Hashes via “each” in a “while” 14

15 to be continued... http://webcert.kirkwood.edu/~fmcclurg/cour ses/php/slides/chapter06c.multidimential.ppt


Download ppt "Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1."

Similar presentations


Ads by Google