Download presentation
Presentation is loading. Please wait.
1
Kirkwood Center for Continuing Education
Introduction to PHP and MySQL Kirkwood Center for Continuing Education By Fred McClurg, © Copyright 2016, Fred McClurg, All Rights Reserved.
2
Chapter Six Associative Arrays
3
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;
4
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?
5
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?
6
Looping Associative Arrays
Chapter Six Looping Associative Arrays
7
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.
8
The “foreach” Hash Array Syntax
foreach ( $array as $key => $value ) { statement; ... }
9
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?
10
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.
11
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 ); } ?>
12
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] ); }
13
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.
14
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 ); } ?>
15
to be continued ...
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.