英[slaɪs] 美[slaɪs]

vt. Cut into slices; cut off; divide

n. Thin slice; part; (a curveball hit due to a mistake)

vi. Slash

Third person singular: slices Plural: slices Present participle: slicing Past tense: sliced ​​Past participle: sliced

php array_slice() function syntax

Function: Retrieve a value from the array based on conditions and return

Syntax: array_slice(array,start,length,preserve)

Parameters:

ParametersDescription
arrayRequired. Specifies an array.
start Required. numerical value. Specifies the starting position of the element to be retrieved. 0 = first element. If the value is set to a positive number, it will be taken from front to back. If the value is set to a negative number, the absolute value of start is taken from back to front. -2 means start from the second to last element of the array.
length Optional. numerical value. Specifies the length of the returned array. If the value is set to an integer, that number of elements is returned. If this value is set to a negative number, the function will terminate fetching this far from the end of the example array. If this value is not set, all elements starting from the position set by the start parameter to the end of the array are returned.
preserveOptional. Specifies whether the function retains key names or resets key names. Possible values: true - keep key names, false - default. Reset key name

Note: If the array has a string key, the returned array will retain the key name.

php array_slice() function example

<?php
$class=array("西门","灭绝","无忌","peter");
print_r(array_slice($class,0,2));
?>

Run instance»

Click the "Run instance" button to view the online instance

Output:

Array ( [0] => 西门 [1] => 灭绝 )


<?php
$a=array("灭绝","西门","无忌","欧阳克","韦小宝");
print_r(array_slice($a,1,2,true));
?>

Run Instance»

Click the "Run Instance" button to view the online instance

Output:

Array ( [1] => 西门 [2] => 无忌 )