PHP and MySQL

Introduction to

PHP and MySQL

Kirkwood Center for

Continuing Education

By Fred McClurg, frmcclurg@gmail.com

© Copyright 2010 All Rights Reserved.

1

Chapter Six

Hurray for Array Functions

http://webcert.kirkwood.edu/~fmcclurg

/courses/php/slides/chapter06.functions.

ppt

2

Array Function: “array_push”

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<br />",

$i, $obj[$i] );

3

Array Function: “array_pop”

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<br />",

$i, $stooge[$i] );

?> printf( "<p />" ); printf( "\$best: %s", $best );

4

Array Function: “array_unshift”

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<br />",

$i, $stooge[$i] );

5

Array Function: “array_shift”

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<br />",

$i, $narnia[$i] );

?> printf( "\$highKing: %s", $highKing );

6

Array Function: “shuffle”

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<br />",

$i, $deck[$i] );

7

Array Function: “sort”

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<br />",

$i, $torah[$i] );

8

Array Function: “array_reverse”

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;

9

Array Function: “array_splice”

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<br />", $drawn[0] ); printf( "Remaining deck:<br />" );

?> for ( $i = 0; $i < count($deck); $i++ ) printf( "\$deck[%d]: %s<br />",

$i, $deck[$i] );

10

Don’t get angry, just walk array!

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' );

11

“array_walk” Example

<?php

function printRow( $value, $key )

{ printf( "

<tr>

<td> $key </td>

<td> $value </td>

</tr>\n" );

}

$color = array( 'Red' => '#FF0000',

'Green' => '#00FF00',

'Blue' => '#0000FF',

'Yellow' => '#FFFF00' );

?> array_walk( $color, 'printRow' );

12

Student Exercise 6.3 Array Functions

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()”.

13

Student Solution 6.3 Array Functions

<?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<br />

Scrambled: %s<br />",

$word, $scramble );

14

Student Exercise 6.4 Array Functions

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”.

15

Student Solution 6.4 Array Functions

<?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<p />

Scrambled: %s", $sentence, $scramble );

16

to be continued ...

http://webcert.kirkwood.edu/~fmcclurg/cour ses/php/slides/chapter08a.fundamentals.ppt