Home > Article > Backend Development > Introduction to the usage of array_slice function in PHP_PHP tutorial
This article specifically introduces the detailed usage of the array_slice function and some commonly used array_slice example programs. Students who need to know more can take a look.
The array_slice() function removes a segment of value from the array based on conditions and returns it.
Note: If the array has string keys, the returned array will retain the key names. (see example 4)
Grammar
array_slice(array,offset,length,preserve)
The function takes out a value from the array based on conditions and returns
Parameters
array required. Specifies the input array.
offset required. numerical value. Specifies the starting position of the element to be retrieved. If it is a positive number, it is taken from the front to the back. If it is a negative value, the offset absolute value is taken from the back to the front.
length is optional. numerical value. Specifies the length of the returned array. If it is a negative number, select the absolute number of elements of the value from back to front. If the value is not set, all elements are returned.
preserve is optional. Possible values: true – retain key false – default – reset key
When it is 0, assign the value inside to a new variable, and finally return this variable.
I flipped through the manual while I was idle today, and it turns out that this thing has a ready-made function: array_slice.
The code is as follows | Copy code | ||||||||
var_dump(array_slice($arr,0,2));
|
The code is as follows | Copy code |
array(2) { [0] => int(0) [1]=> int(1) } array(1) { ["a"]=> array(3) { [0]=> string(1) "a" [1]=> string(1) "a" [2]=> string(1) "a" } } |
I found the array_slice function. Very useful, share it:
The code is as follows
|