Prev | Current Page 224 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

Its prototype follows:
array array_slice(array array, int offset [, int length])
A positive offset value will cause the slice to begin offset positions from the beginning
of the array, while a negative offset value will start the slice offset positions from
the end of the array. If the optional length parameter is omitted, the slice will start at
offset and end at the last element of the array. If length is provided and is positive, it
will end at offset + length positions from the beginning of the array. Conversely, if
length is provided and is negative, it will end at count(input_array) ??“ length positions
from the end of the array. Consider an example:
$states = array("Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Colorado", "Connecticut");
$subset = array_slice($states, 4);
print_r($subset);
156 CHAPTER 5 ?–  ARRAYS
This returns the following:
Array ( [0] => California [1] => Colorado [2] => Connecticut )
Consider a second example, this one involving a negative length:
$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 )
Splicing an Array
The array_splice() function removes all elements of an array found within a specified
range, returning those removed elements in the form of an array.


Pages:
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236