Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 http://cecert.kirkwood.edu/~fmcclurg/courses/ph p/slides/chapter06c.multidimential.ppt Chapter Six Multidimential Arrays and Hashes 2

3 Multidimensional Array Element Initialization Example: <?php for ( $i = 0; $i < 3; $i++ ) { for ( $j = 0; $j < 3; $j++ ) { // multiplication table $grid[$i][$j] = $i * $j; } print_r( $grid ); ?> Multidimensional Numeric Array 3

4 Multidimensional Hash Element Initialization Example: <?php $pop['IA']['Independence'] = 6101; $pop['IA']['Manchester'] = 4898; $pop['KS']['Independence'] = 9277; $pop['KS']['Manchester'] = 100; $pop['MO']['Independence'] = 110704; $pop['MO']['Manchester'] = 18657; printf( "print_r( \$pop ); " ); print_r( $pop ); printf( " " ); printf( "var_dump( \$pop ); " ); var_dump( $pop ); printf( " " ); printf( "var_export( \$pop ); " ); var_export( $pop ); ?> Multidimensional Hash Array 4

5 Array of Arrays Initialization Construct Example: <?php $grid = array( array( 0 * 0, 0 * 1, 0 * 2 ), array( 1 * 0, 1 * 1, 1 * 2 ), array( 2 * 0, 2 * 1, 2 * 2 ) ); echo " "; print_r( $grid ); echo " "; ?> Array of Arrays 5

6 Hash of Arrays Initialization Construct Example: <?php $bedrock = array( 'Flintstone' => array( 'Fred', 'Wilma', 'Pebbles', 'Dino' ), 'Rubble' => array( 'Barney', 'Betty', 'Bambam' ) ); foreach ( $bedrock as $last => $family ) { echo "\$last: $last "; foreach ( $family as $first ) { echo " \$first: $first "; } ?> Hash of Arrays 6

7 Description: Recursion is a function that calls itself. Advantage: Elegant solution. Disadvantage: Requires more memory. Pitfall: Exit condition must be well defined. What is Recursion? 7

8 Hash of Hashes example using recursion to print key/value pairs: <?php function PrintArray( $array, $indent ) { foreach( $array as $key => $value ) { $spacer = str_repeat(" ", $indent ); printf( "%s Key: %s", $spacer, $key ); if ( is_array( $value ) ) { printf( " " ); PrintArray( $value, $indent + 1 ); // recursive call } else { printf( " Value: %s ", $value ); } } // PrintArray... ?> Hash of Hashes, Recursive Function (1) 8

9 Hash of Hashes example using recursion to print key/value pairs: <?php... /* array defined via single statement below * * $popByZip['IA']['Ames']['50010'] = 24991; * $popByZip['IA']['Ames']['50014'] = 29541; * $popByZip['IA']['Dubuque']['52001'] = 44033; * $popByZip['IA']['Dubuque']['52002'] = 11539; * $popByZip['IA']['Dubuque']['52003'] = 13305; * $popByZip['IA']['Waterloo']['50701'] = 29890; * $popByZip['IA']['Waterloo']['50702'] = 19299; * $popByZip['IA']['Waterloo']['50703'] = 20978; * $popByZip['IA']['Waterloo']['50707'] = 8155; */... ?> Hash of Hashes, Multiple Init (2) 9

10 Hash of Hashes example using recursion to print key/value pairs: <?php // array can also be defined via multiple statements above $popByZip = array( 'IA' => array( 'Ames' => array( 50010 => 24991, 50014 => 29541 ), 'Dubuque' => array( 52001 => 44033, 52002 => 11539, 52003 => 13305 ), 'Waterloo' => array( 50701 => 29890, 50702 => 19299, 50703 => 20978, 50707 => 8155 ) ) ); PrintArray( $popByZip, 0 ); ?> Hash of Hashes, Single Init (3) 10

11 to be continued... http://cecert.kirkwood.edu/~fmcclurg/courses/ php/slides/chapter06d.printing.ppt11


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

Similar presentations


Ads by Google