Prev | Current Page 225 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

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 the array, while a negative offset will start the splice that many positions
from the end of the array. If the optional length parameter is omitted, all elements
from the offset position to the conclusion of the array will be removed. If length is
provided and is positive, the splice will end at offset + length positions from the beginning
of the array. Conversely, if length is provided and is negative, the splice will end
at count(input_array) ??“ length positions from the end of the array. An example follows:
$states = array("Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Connecticut");
$subset = array_splice($states, 4);
CHAPTER 5 ?–  ARRAYS 157
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 target segment. An example follows:
$states = array("Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Connecticut");
$subset = array_splice($states, 2, -1, array("New York", "Florida"));
print_r($states);
This returns the following:
Array ( [0] => Alabama [1] => Alaska [2] => New York
[3] => Florida [4] => Connecticut )
Calculating an Array Intersection
The array_intersect() function returns a key-preserved array consisting only of those
values present in the first array that are also present in each of the other input arrays.


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