In daily PHP programming, we often need to extract a portion of the elements from an array for displaying, paginating, or processing subset data. PHP provides a very practical function - array_slice() , which can help us complete this task efficiently.
array_slice() is a function in PHP used to extract a sub-array from an array. It does not change the original array, but returns the new array of the intercepted part. The basic syntax is as follows:
array_slice(array $array, int $offset, ?int $length = null, bool $preserve_keys = false): array
$array : original array.
$offset : where to start intercepting. If it is a negative number, it starts with the reciprocating at the end of the array.
$length (optional): The number of elements to be intercepted. If omitted, it will be intercepted from offset to the end of the array.
$preserve_keys (optional): Whether to retain the original key name, the default is false .
$fruits = ['apple', 'banana', 'orange', 'grape', 'melon'];
$sliced = array_slice($fruits, 1, 3);
print_r($sliced);
Output:
Array
(
[0] => banana
[1] => orange
[2] => grape
)
Starting from subscript 1, intercept 3 elements. Note that the key names are reordered, because preserve_keys defaults to false .
$fruits = ['a' => 'apple', 'b' => 'banana', 'c' => 'orange', 'd' => 'grape'];
$sliced = array_slice($fruits, 1, 2, true);
print_r($sliced);
Output:
Array
(
[b] => banana
[c] => orange
)
Here we set preserve_keys to true , so the key names in the original array are preserved.
$numbers = [10, 20, 30, 40, 50];
$sliced = array_slice($numbers, -3, 2);
print_r($sliced);
Output:
Array
(
[0] => 30
[1] => 40
)
Use negative offset to intercept 2 from the third last element at the end of the array.
Suppose we read an array containing multiple article titles from the database, and now we need to implement paging, showing 5 pieces of data per page:
$articles = [
'article1', 'article2', 'article3', 'article4', 'article5',
'article6', 'article7', 'article8', 'article9', 'article10',
'article11', 'article12'
];
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$pageSize = 5;
$offset = ($page - 1) * $pageSize;
$pagedArticles = array_slice($articles, $offset, $pageSize);
foreach ($pagedArticles as $article) {
echo "<p>$article</p>";
}
// Sample paging link
echo '<a href="https://gitbox.net/articles.php?page=1">front page</a> ';
echo '<a href="https://gitbox.net/articles.php?page=2">1.2Page</a> ';
echo '<a href="https://gitbox.net/articles.php?page=3">1.3Page</a>';
The above code shows how to use array_slice() combined with $_GET parameter to implement simple paging function and generate paging links. Note that the URL here uses the gitbox.net domain name.
array_slice() is an indispensable tool for handling array interception operations. It is flexible, efficient and simple to use. By rationally using offset, length and preserve_keys parameters, we can quickly obtain the required subset of arrays based on different scenarios. Whether in data paging, list display or data filtering, array_slice() can show off its skills.