PHP之array_slice()注意点

Definition and Usage

The array_slice() function returns selected parts of an array.

Note: If the array have string keys, the returned array will always preserve the keys.

 

Syntax

array_slice(array,start,length,preserve)

Parameter Description
array Required. Specifies an array
start Required. Numeric value. Specifies where the function will start the slice. 0 = the first element. If this value is set to a negative number, the function will start slicing that far from the last element. -2 means start at the second last element of the array.
length Optional. Numeric value. Specifies the length of the returned array. If this value is set to a negative number, the function will stop slicing that far from the last element. If this value is not set, the function will return all elements, starting from the position set by the start-parameter.
preserve Optional. Specifies if the function should preserve or reset the keys. Possible values: 

  • true –  Preserve keys
  • false – Default. Reset keys

 

Examples

Start the slice from from the second array element, and return only two elements:

With the preserve parameter set to true:

With both string and integer keys:

摘自:http://www.w3schools.com/php/func_array_slice.asp

发表评论