Home > Article > Backend Development > Detailed explanation of array_slice function usage examples in PHP, array_slice examples_PHP tutorial
This article introduces the detailed usage of the array_slice function and some commonly used array_slice example programs, sharing it with everyone for your reference. The specific analysis is as follows:
The array_slice() function takes out a segment of value from the array based on conditions and returns.
Note: If the array has string keys, the returned array will retain the key names, see Example 4.
Syntax: 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 is required, a numerical value, which specifies the starting position of the element to be taken out. If it is a positive number, it will be taken from the front to the back. If it is a negative value, the offset absolute value will be taken from the back to the front.
length Optional, numerical value, specifies the length of the returned array. If it is a negative number, the absolute number of elements of the value will be selected from back to front. If the value is not set, all elements will be returned.
preserve is optional, possible values: true - reserved key false - default - reset key, when it is 0, assign the value inside to a new variable, and finally return this variable.
The code is as follows:
The returned results are as follows:
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" } }
The paging method implemented by the array_slice function is very easy to use, share it as follows:
name | sex | job |
希望本文所述对大家的PHP程序设计有所帮助。