Array Function: “array_push” 3"> Array Function: “array_push” 3">

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 Hurray for Array Functions 2 http://webcert.kirkwood.edu/~fmcclurg /courses/php/slides/chapter06.functions. ppt

3 Description: Adds one or more elements to the end of an array. Syntax: array_push( &$array, $elements ); Example: <?php $obj = array( "rock" ); array_push( $obj, "paper", "scissors" ); for ( $i = 0; $i < count($obj); $i++ ) printf( "\$obj[%d]: %s ", $i, $obj[$i] ); ?> Array Function: “array_push” 3

4 Description: Removes the last element of an array and returns that value. Syntax: $last = array_pop( &$array ); Example: <?php $stooge = array( "Moe", "Larry", "Curly" ); $best = array_pop( $stooge ); for ( $i = 0; $i < count($stooge); $i++ ) printf( "\$stooge[%d]: %s ", $i, $stooge[$i] ); printf( " " ); printf( "\$best: %s", $best ); ?> Array Function: “array_pop” 4

5 Description: Adds one or more elements to the beginning of an array. Syntax: array_unshift( &$array, $elements ); Example: <?php $stooge = array( "Moe", "Larry" ); array_unshift( $stooge, "Shemp" ); for ( $i = 0; $i < count($stooge); $i++ ) printf( "\$stooge[%d]: %s ", $i, $stooge[$i] ); ?> Array Function: “array_unshift” 5

6 Description: Removes the first element of an array and returns that value. Syntax: $first = array_shift( &$array ); Example: <?php $narnia = array( "Peter", "Susan", "Lucy", "Edmund" ); $highKing = array_shift( $narnia ); for ( $i = 0; $i < count($narnia); $i++ ) printf( "\$narnia[%d]: %s ", $i, $narnia[$i] ); printf( "\$highKing: %s", $highKing ); ?> Array Function: “array_shift” 6

7 Description: Arranges array values in random order. Syntax: shuffle( &$array ); Example: <?php $deck = array( "Ace", "King", "Queen", "Jack", "Joker" ); shuffle( $deck ); // randomize array for ( $i = 0; $i < count($deck); $i++ ) printf( "\$deck[%d]: %s ", $i, $deck[$i] ); ?> Array Function: “shuffle” 7

8 Description: Sorts the array in alphabetical order. Syntax: sort( &$array ); Example: <?php $torah = array( "Genesis", "Exodus", "Leviticus", "Numbers", "Deuteronomy" ); sort( $torah ); // sort array by value for ( $i = 0; $i < count($torah); $i++ ) printf( "\$torah[%d]: %s ", $i, $torah[$i] ); ?> Array Function: “sort” 8

9 Description: Reverses the order of the array values. Syntax: $arrayNew = array_reverse( $arrayOld ); Example: <?php $name = "rumpelstiltskin"; // convert string to array $pieces = str_split( $name ); // reverse the array order $reverse = array_reverse( $pieces ); // convert array back to string $backward = implode( "", $reverse ); echo $backward; ?> Array Function: “array_reverse” 9

10 Description: Removes a portion of an array and replace it with something else. Syntax: $aryRm = array_splice( &$input, $offset[, $length=0 [, $replacement]] ); Example: <?php $deck = array( "Ace", "King", "Queen", "Jack", "Joker" ); $max = count($deck) - 1; $offset = rand( 0, $max ); // draw one random card $drawn = array_splice( $deck, $offset, 1 ); printf( "Card drawn: %s ", $drawn[0] ); printf( "Remaining deck: " ); for ( $i = 0; $i < count($deck); $i++ ) printf( "\$deck[%d]: %s ", $i, $deck[$i] ); ?> Array Function: “array_splice” 10

11 array_walk(): Send value/key pairs of an associative array to be handled by a user defined function. User Defined Function Syntax: <?php function functName( $value, $key ) {... } array_walk( $hash, 'functName' ); ?> Don’t get angry, just walk array! 11

12 <?php function printRow( $value, $key ) { printf( " $key $value \n" ); } $color = array( 'Red' => '#FF0000', 'Green' => '#00FF00', 'Blue' => '#0000FF', 'Yellow' => '#FFFF00' ); array_walk( $color, 'printRow' ); ?> “array_walk” Example 12

13 string Problem: Write a string scramble simulation game. The order of the characters shall be randomized. Input: Initialize a variable to a word or string to be scrambled. Output: Display the original string and the randomized string to the screen. Hint One: Use the predefined functions, “str_split()”, “shuffle()” and “implode()” to make the task easier. Hint Two: See example for “array_reverse()”. Student Exercise 6.3 Array Functions 13

14 <?php $word = "randomize"; // break each char into array $pieces = str_split( $word ); // randomize the array shuffle( $pieces ); // convert elements into string $scramble = implode( "", $pieces ); printf( "Original: %s Scrambled: %s ", $word, $scramble ); ?> Student Solution 6.3 Student Solution 6.3 Array Functions 14

15 sentence Problem: Write a sentence scramble simulation game. The order of the words shall be randomized. The words themselves shall not be scrambled. Input: Initialize a variable to number of words or a sentence to be scrambled. Output: Display the original sentence and the randomized sentence to the screen. Hint One: Solution will be similar to Exercise 6.2 Hint Two: Use predefined function “explode” instead of “str_split”. Student Exercise 6.4 Array Functions 15

16 <?php $sentence = "Now is the time for all good men to pray for the aid of their country."; // at a space, break each word into an array $words = explode( " ", $sentence ); // randomize the array shuffle( $words ); // join elements into string with spaces $scramble = implode( " ", $words ); printf( "Original: %s Scrambled: %s", $sentence, $scramble ); ?> Student Solution 6.4 Array Functions 16

17 to be continued... http://webcert.kirkwood.edu/~fmcclurg/cour ses/php/slides/chapter08a.fundamentals.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