11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 5 • ARRAYSwww.it-ebooks.infoMoving the Pointer to the First Array PositionThe reset() function serves to set an array pointer back to the beginning of the array. Its prototypefollows:mixed reset(array array)This function is commonly used when you need to review or manipulate an array multiple timeswithin a script, or when sorting has completed.Moving the Pointer to the Last Array PositionThe end() function moves the pointer to the last position of an array, returning the last element. Itsprototype follows:mixed end(array array)The following example demonstrates retrieving the first and last array values:$fruits = array("apple", "orange", "banana");$fruit = current($fruits); // returns "apple"$fruit = end($fruits); // returns "banana"Passing Array Values to a FunctionThe array_walk() function will pass each element of an array to the user-defined function. This is usefulwhen you need to perform a particular action based on each array element. If you intend to actuallymodify the array key/value pairs, you’ll need to pass each key/value to the function as a reference. Itsprototype follows:boolean array_walk(array &array, callback function [, mixed userdata])The user-defined function must take two parameters as input. The first represents the array’scurrent value, and the second represents the current key. If the optional userdata parameter is presentin the call to array_walk(), its value will be passed as a third parameter to the user-defined function.You are probably scratching your head, wondering how this function could possibly be of any use.Perhaps one of the most effective examples involves the sanity-checking of user-supplied form data.Suppose the user is asked to provide six keywords that he thinks best describe the state in which he lives.A sample form is provided in Listing 5-1.Listing 5-1. Using an Array in a FormProvide up to six keywords that you believe best describe the state inwhich you live:Keyword 1:Keyword 2:116

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!