What it is and why to use it
A dashboard may display the first few announcements or the last two activities. array_slice() returns that portion of an array without removing items from the original. Its offset is a zero-based iteration position, not an array key. This distinction matters when the keys are IDs such as 101 and 205.
How it works
A nonnegative offset starts from the beginning; a negative offset counts back from the end. A positive length limits how many entries are returned, while an omitted length keeps the rest. Integer keys are reset by default and string keys are always preserved. Set the fourth argument to true when integer keys must remain meaningful.
Common mistakes and limitations
Do not use array_slice as database pagination after loading a huge table: request a suitably ordered subset from the database instead. Do not confuse it with array_splice, which modifies its input. A page offset must be calculated from the page number and page size, not from a record ID.
Run the examples locally
Save each complete example in its own example.php file and run php example.php in a terminal from that folder. Each example is independent: do not paste both into one file, especially when they declare classes with the same names. Output appears in the terminal. The examples use PHP 8.0-compatible syntax; use a currently supported PHP release for development.
The <?php opening tag starts PHP code. declare(strict_types=1) requests strict scalar argument checks for function calls made from this file; it does not validate external input for you. echo writes output, and PHP_EOL adds the platform’s line ending. Quotation marks delimit strings; they are not part of the printed text unless explicitly included in the string.
These examples use built-in PHP language features and standard extensions. They need no Composer packages, database server, browser APIs or network access. Codectionary’s PHP preview does not execute PHP; use your local interpreter to reproduce the output.
Check your interpreter, lint and run one saved example
php -v
php -l example.php
php example.phpContinue learning
Use the related guides to connect this operation to the surrounding language features. Practise by changing the example inputs and predicting the output before running it.
Syntax
$window = array_slice($items, $offset, $length, $preserveKeys);Examples
Display the second page of a small list
Use a fixed in-memory list to explain the offset calculation.
<?php
declare(strict_types=1);
$lessons = ['Variables', 'Arrays', 'Functions', 'Classes', 'Dates'];
$page = 2;
$pageSize = 2;
$offset = ($page - 1) * $pageSize;
$visible = array_slice($lessons, $offset, $pageSize);
echo implode(', ', $visible), PHP_EOL;
echo count($lessons), PHP_EOL;
- Page one starts at position 0, so page two starts at (2 - 1) * 2, which is 2.
- array_slice reads up to two values from that position: Functions and Classes.
- The original still contains five lessons because slicing returns a separate array. Real pagination must also validate page numbers and define a stable order.
- Expected output: Functions, Classes 5
Keep IDs in a recent-item window
Select the final entry while retaining its numeric identifier.
<?php
declare(strict_types=1);
$articles = [101 => 'Arrays', 205 => 'Objects', 309 => 'Dates'];
$latest = array_slice($articles, -1, 1, true);
foreach ($latest as $id => $title) {
echo $id, ': ', $title, PHP_EOL;
}
- The negative offset -1 starts at the last iteration position, regardless of that item’s key.
- A length of 1 selects just that item, and true preserves key 309.
- This is useful for a small ordered collection of records when callers still need their IDs.
- Expected output: 309: Dates
Best practices
- Distinguish item positions from record identifiers.
- Preserve keys explicitly when returning a keyed subset.
Runtime & versions
PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
PHP Manual