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, © Copyright 2010 All Rights Reserved. 1

2 Chapter Six Hurray for Array Functions 2 /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: Array Function: “array_push” 3

4 Description: Removes the last element of an array and returns that value. Syntax: $last = array_pop( &$array ); Example: Array Function: “array_pop” 4

5 Description: Adds one or more elements to the beginning of an array. Syntax: array_unshift( &$array, $elements ); Example: Array Function: “array_unshift” 5

6 Description: Removes the first element of an array and returns that value. Syntax: $first = array_shift( &$array ); Example: Array Function: “array_shift” 6

7 Description: Arranges array values in random order. Syntax: shuffle( &$array ); Example: Array Function: “shuffle” 7

8 Description: Sorts the array in alphabetical order. Syntax: sort( &$array ); Example: Array Function: “sort” 8

9 Description: Reverses the order of the array values. Syntax: $arrayNew = array_reverse( $arrayOld ); Example: 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: 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: Don’t get angry, just walk array! 11

12 '#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 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 Student Solution 6.4 Array Functions 16

17 to be continued... 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