Array Function: “array_push” 3 What is contained in array “$obj”?"> Array Function: “array_push” 3 What is contained in array “$obj”?">

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 Chapter Six Hurray for Array Functions http://cecert.kirkwood.edu/~fmcclurg/courses/p hp/slides/chapter06e.functions.ppt 2

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

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 What is contained in array “$stooge”?

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

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 What is the contents of array “$naria”?

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 What is the value of array “$deck”?

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 What is the value of array “$torah”?

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 What is the value of variable “$backward”?

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 What are the values in array “$deck”?

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 ) function functName( $value, $key ) {...... } array_walk( $hash, 'functName' ); array_walk( $hash, 'functName' );?> Don’t get angry, just walk array! 11

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

13 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 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 What does the string “$scramble” contain?

15 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."; $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 // at a space, break each word into an array $words = explode( " ", $sentence ); $words = explode( " ", $sentence ); // randomize the array // randomize the array shuffle( $words ); shuffle( $words ); // join elements into string with spaces // join elements into string with spaces $scramble = implode( " ", $words ); $scramble = implode( " ", $words ); printf( "Original: %s printf( "Original: %s Scrambled: %s", $sentence, $scramble ); Scrambled: %s", $sentence, $scramble );?> Student Solution 6.4 Array Functions 16

17 to be continued... http://cecert.kirkwood.edu/~fmcclurg/courses/ph p/slides/chapter08a.fundamentals.ppt http://cecert.kirkwood.edu/~fmcclurg/courses/ph p/slides/chapter08a.fundamentals.ppt17


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