In PHP, the array_slice function is a very practical array operation function for intercepting a sub-array from an array. Understanding the positive and negative meaning of its parameters offset and length is crucial for flexible manipulation of arrays. This article will analyze the meaning of these two parameters in detail and combine examples to help you master their usage.
The basic syntax of the array_slice function is as follows:
array_slice(array $array, int $offset, int $length = null, bool $preserve_keys = false): array
$array : The array to be intercepted.
$offset : The start position of the intercept.
$length : The intercepted length (optional).
$preserve_keys : Whether to preserve the key name of the original array (default is not preserved, index reset).
The offset parameter specifies the position of the intercepting starting point, which can be a positive or negative number.
Positive offset : starts from the beginning of the array, 0 means the first element, 1 means the second element, and so on.
Negative offset : starts from the end of the array, -1 represents the last element, -2 represents the second-last element, and so on.
$array = ['a', 'b', 'c', 'd', 'e'];
// offset = 2,Starting from the third element
print_r(array_slice($array, 2));
// result: ['c', 'd', 'e']
// offset = -2,Starting from the penultimate element
print_r(array_slice($array, -2));
// result: ['d', 'e']
The length parameter specifies the number of intercepted elements, and it can also be positive and negative integers:
Positive length : Intercepts the specified number of elements.
Negative length : The specified number of elements before the end of the array is intercepted, that is, several elements at the end are excluded.
If length is omitted, all elements from offset to the end of the array are intercepted by default.
$array = ['a', 'b', 'c', 'd', 'e'];
// offset = 1, length = 3,Intercepting the starting point from the second element3Elements
print_r(array_slice($array, 1, 3));
// result: ['b', 'c', 'd']
// offset = 1, length = -1,InterceptFrom二Elements开始,Exclude the last一Elements
print_r(array_slice($array, 1, -1));
// result: ['b', 'c', 'd']
When offset is positive and length is negative, length means the number of elements at the end of the array, and the intercept range is to subtract abs(length) from offset to the end of the array.
When offset is negative and length is positive, intercept the length elements starting from the penultimate abs(offset) elements.
When the absolute value of length exceeds the number of remaining elements in the array, the function automatically intercepts the array boundary and will not report an error.
By default, the array returned by array_slice is reindexed (starting from 0). If you want to retain the key name of the original array, you can set the fourth parameter to true .
$array = ['a' => 'apple', 'b' => 'banana', 'c' => 'cherry'];
// No key names are retained
print_r(array_slice($array, 1, 2));
// result: [0 => 'banana', 1 => 'cherry']
// Reserved key names
print_r(array_slice($array, 1, 2, true));
// result: ['b' => 'banana', 'c' => 'cherry']
$array = [10, 20, 30, 40, 50, 60];
// From2Elements开始Intercept3Elements
$result1 = array_slice($array, 1, 3);
print_r($result1);
// Output: [20, 30, 40]
// From the last4Elements开始,Snatch to the last2Elements(Exclude the last1Elements)
$result2 = array_slice($array, -4, -1);
print_r($result2);
// Output: [30, 40, 50]
// From the last3Elements开始,Intercept2Elements
$result3 = array_slice($array, -3, 2);
print_r($result3);
// Output: [40, 50]
// From3Elements开始Intercept所有元素,Reserved key names
$result4 = array_slice($array, 2, null, true);
print_r($result4);
// Output: [2 => 30, 3 => 40, 4 => 50, 5 => 60]
Suppose we have a set of URL lists, we need to intercept some URLs and replace the domain name with gitbox.net .
$urls = [
'https://example.com/page1',
'https://example.com/page2',
'https://example.com/page3',
'https://example.com/page4',
];
// From二个URL开始Intercept两个
$slicedUrls = array_slice($urls, 1, 2);
// Replace domain name
$processedUrls = array_map(function($url) {
return preg_replace('#https?://[^/]+#', 'https://gitbox.net', $url);
}, $slicedUrls);
print_r($processedUrls);
// Output:
// [
// 'https://gitbox.net/page2',
// 'https://gitbox.net/page3'
// ]