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.info$states = array("Alabama", "Alaska", "Arizona", "Arkansas","California", "Colorado", "Connecticut");$subset = array_slice($states, 2, -2);print_r($subset);This returns the following:Array ( [0] => Arizona [1] => Arkansas [2] => California )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.Splicing an ArrayThe array_splice() function removes all elements of an array found within a specified range, returningthose removed elements in the form of an array. Its prototype follows:array array_splice(array array, int offset [, int length [, array replacement]])A positive offset value will cause the splice to begin that many positions from the beginning of thearray, while a negative offset will start the splice that many positions from the end of the array. If theoptional length parameter is omitted, all elements from the offset position to the conclusion of the arraywill be removed. If length is provided and is positive, the splice will end at offset + length position fromthe beginning of the array. Conversely, if length is provided and is negative, the splice will end atcount(input_array) – length position from the end of the array. An example follows:$states = array("Alabama", "Alaska", "Arizona", "Arkansas","California", "Connecticut");$subset = array_splice($states, 4);print_r($states);print_r($subset);This produces the following (formatted for readability):Array ( [0] => Alabama [1] => Alaska [2] => Arizona [3] => Arkansas )Array ( [0] => California [1] => Connecticut )You can use the optional parameter replacement to specify an array that will replace the targetsegment. An example follows:$states = array("Alabama", "Alaska", "Arizona", "Arkansas","California", "Connecticut");128

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

Saved successfully!

Ooh no, something went wrong!