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.

www.it-ebooks.infoCHAPTER 5 • ARRAYSRetrieving Array KeysThe array_keys() function returns an array consisting of all keys located in an array. Its prototypefollows:array array_keys(array array [, mixed search_value [, boolean preserve_keys]])If the optional search_value parameter is included, only keys matching that value will be returned.The following example outputs all of the key values found in the $state array:$state["Delaware"] = "December 7, 1787";$state["Pennsylvania"] = "December 12, 1787";$state["New Jersey"] = "December 18, 1787";$keys = array_keys($state);print_r($keys);The output follows:Array ( [0] => Delaware [1] => Pennsylvania [2] => New Jersey )Setting the optional preserve_keys parameter (introduced in <strong>PHP</strong> 5.0.2) to true will cause the arrayvalues’ keys to be preserved in the returned array.Retrieving Array ValuesThe array_values() function returns all values located in an array, automatically providing numericindexes for the returned array. Its prototype follows:array array_values(array array)The following example will retrieve the population numbers for all of the states found in$population:$population = array("Ohio" => "11,421,267", "Iowa" => "2,936,760");print_r(array_values($population));This example will output the following:Array ( [0] => 11,421,267 [1] => 2,936,760 )Traversing ArraysThe need to travel across an array and retrieve various keys, values, or both is common, so it’s not asurprise that <strong>PHP</strong> offers numerous functions suited to this need. Many of these functions do double113

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

Saved successfully!

Ooh no, something went wrong!