Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1
Chapter Six Array: A drop of golden sun or “Hi Ho Silver, Array!” or Up, up and Array ourses/php/slides/chapter06.numeric.ppt 2
Array: Used for storing a collection of values that are referenced by a common variable name and an index value. Analogy: Weekly pill box with “SMTWTFS” on individual lids. Element: The individual data values that are stored in an array. Index: The reference used to access the element location. Array Definition of Terms 3
Chapter Six Numeric Arrays 4
Description: Numeric arrays in PHP are zero based. This means the first array index starts at zero. In other words, the first element uses index zero, the second element is at index one, etc. Pitfall: The last index of the array is the number of elements minus one. Failure to take this into consideration results in the common “off by one” error. Numeric Arrays 5
Initialization via multiple statements: $weekday[] = "Sunday"; $weekday[] = "Monday"; $weekday[] = "Tuesday"; Same As: $weekday[0] = "Sunday"; $weekday[1] = "Monday"; $weekday[2] = "Tuesday"; Numeric Array Element Initialization 6
Array Initialization via a single statement: $weekday = array( "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ); Same As: $weekday[0] = "Sunday"; $weekday[1] = "Monday"; $weekday[2] = "Tuesday";... Numeric Array Initialization Construct 7
Chapter Six Looping Numeric Indexed Arrays 8
Example: <?php $color = array( "red", "green", "blue" ); // obtain array length $size = count( $color ); for ( $i = 0; $i < $size; $i++ ) { printf( "\$color[%d]: %s ", $i, $color[$i] ); } ?> “for” with Numeric Index Array 9
Description: The “foreach” construct was specifically designed to work with arrays. It functions as an iterator that traverses each element of the array sequentially. Preferred Usage: 1. When the element value is needed. 2. When the associated element index number is not used. 3. When every element of the array needs to be accessed. “foreach” with Numeric Arrays 10
Syntax: foreach ( $array as $value ) { statement;... } “foreach” Numeric Array Syntax 11
Example: <?php $colors = array( "red", "green", "blue" ); foreach ( $colors as $name ) echo "$name "; ?> “foreach” Numeric Index Example 12
Assignment: Write a PHP program that prints the ROT13 values for letters of the alphabet. Output: Should look similar to the following: a = n b = o... z = m Hint: Use the “range()” function to initialize array. Exercise 6.1 Numeric Array 13
<?php $letters = range( 'a', 'z' ); foreach ( $letters as $chr ) { if ( $chr > 'm' ) $rot13 = ord( $chr ) - 13; else $rot13 = ord( $chr ) + 13; printf( "%s = %s ", $chr, chr( $rot13 ) ); } ?> Solution 6.1a Numeric Array 14
<?php $letters = range( 'a', 'z' ); foreach ( $letters as $chr ) { $rot13 = str_rot13($chr); printf( "%s = %s ", $chr, $rot13 ); } ?> Solution 6.1b Numeric Array 15
Example: <?php function printList( $list ) { foreach( $list as $item ) { printf( "%s ", $item ); } $groceries = array( "blueberries", "strawberries", "bananas" ); printList( $groceries ); ?> Passing an Array to a Function 16
to be continued... urses/php/slides/chapter06b.associative.ppt