Kirkwood Center for Continuing Education

Slides:



Advertisements
Similar presentations
Line Efficiency     Percentage Month Today’s Date
Advertisements

Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2015, Fred McClurg, All Rights.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2015, Fred McClurg, All Rights.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2015, Fred McClurg, All Rights.
ProjectImpactResourcesDeadlineResourcesDeadline Forecast Plan Time Resources Risk 001xx 002xx 003xx 004xx 005xx 006xx 007xx TotalXX Example 1: Portfolio.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2016, Fred McClurg, All Rights.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2016, Fred McClurg, All Rights.
13 Arrays CE : Fundamental Programming Techniques June 161.
SPOUSE LEADERSHIP DEVELOPMENT COURSE (SLDC) CLASS 68
Jan 2016 Solar Lunar Data.

PHP Arrays Functions.
Q1 Jan Feb Mar ENTER TEXT HERE Notes
Kirkwood Center for Continuing Education

Project timeline # 3 Step # 3 is about x, y and z # 2
Average Monthly Temperature and Rainfall




Self-Insurance Application Schedule – Key Dates*
Mammoth Caves National Park, Kentucky
2017 Jan Sun Mon Tue Wed Thu Fri Sat


Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Free PPT Diagrams : ALLPPT.com


Step 3 Step 2 Step 1 Put your text here Put your text here
Calendar Year 2009 Insure Oklahoma Total & Projected Enrollment
MONTH CYCLE BEGINS CYCLE ENDS DUE TO FINANCE JUL /2/2015
Jan Sun Mon Tue Wed Thu Fri Sat

©G Dear 2008 – Not to be sold/Free to use
Electricity Cost and Use – FY 2016 and FY 2017



Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Free PPT Diagrams : ALLPPT.com


Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Belem Climate Data Table
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Project timeline # 3 Step # 3 is about x, y and z # 2

TIMELINE NAME OF PROJECT Today 2016 Jan Feb Mar Apr May Jun

Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Pilot of revised survey
Presentation transcript:

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

Chapter Six Associative Arrays http://cecert.kirkwood.edu/~fmcclurg/courses/php/slides/chapter06b.associative.ppt

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;

Associative Array Element Initialization 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<br>", $name, $days ); } ?> Note: The internal order of an associative array can not be guaranteed. What would be printed?

Hash Array Initialization Construct 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<br>", $uname, $fname ); } ?> Note: The internal order of an associative array can not be guaranteed. What would be printed?

Looping Associative Arrays Chapter Six Looping Associative Arrays

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: When the element value and the element key is needed. When an operation is performed on every element.

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

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

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: In one statement, initialize an associative array to the zip code and city name. (ex: KW = 52404, NL = 52317, CV = 52241) Use a foreach operator to print the zip code and the associated city name.

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

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 ); } ?> Question: How would you print out the months in order? One Way To Do It (OWTDI): // define an array with the months in order $sorted = array( "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" ); ... foreach ( $sorted as $name ) { printf( "%s has %d days<br>", $name, $month[$name] ); }

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 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 ); } ?>

to be continued ... http://cecert.kirkwood.edu/~fmcclurg/courses/php/slides/chapter06c.multidimential.ppt