When writing PHP applications, you may need to extract a portion of an array without changing the original. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.
The array_slice() operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.
What Is array_slice() in PHP?
Extract a portion of an array without changing the original.
Its syntax is:
array_slice(array $array, int $offset, ?int $length = null, bool $preserve_keys = false): array
Parameters used by this operation:
$array— The collection being examined or updated.$offset— The starting position in the collection.$length— The number of entries to consider, when provided.$preserve_keys— The value supplied for this argument affects the operation.
Negative offsets count from the end. String keys are retained; numeric keys normally reset unless preserve_keys=true.
Example 1: Basic Usage
Start with a small, self-contained input so you can inspect exactly how the operation behaves.
<?php
$products = ["pen", "paper", "folder", "tape"];
print_r(array_slice($products, 1, 2));
Output:
Array
(
[0] => paper
[1] => folder
)
The output shows the result of the first call. Check the printed keys and values as well as their order.
Example 2: Another Important Case
This variation highlights another input shape, a comparison option, or a change in the collection.
<?php
$events = ["login", "edit", "save", "logout"];
print_r(array_slice($events, -2));
Output:
Array
(
[0] => save
[1] => logout
)
Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.
Practical Example: Select a page of already-fetched records for a simple preview
Select a page of already-fetched records for a simple preview. Save the code in a PHP file and adapt the sample data to your project.
<?php
$allTickets = ["T-101", "T-102", "T-103", "T-104", "T-105"];
$pageNumber = 2; $pageSize = 2;
$pageTickets = array_slice($allTickets, ($pageNumber - 1) * $pageSize, $pageSize);
foreach ($pageTickets as $ticket) { echo $ticket . PHP_EOL; }
Output:
T-103
T-104
The output comes from the completed workflow shown above. When adapting it, change the source records and keep the same checks for missing, unexpected, or invalid values.
Important Behavior and Edge Cases
Negative offsets count from the end. String keys are retained; numeric keys normally reset unless preserve_keys=true.
- Test the operation with empty or missing input and decide how your application should respond.
- Read the return value independently from any changes to variables passed by reference.
- Validate untrusted input before using the result for permissions, database changes, or user-visible output.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
array_slice() | Extract a portion of an array without changing the original |
| Related functions | array_splice() removes or replaces the selected part and modifies the input. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Assuming input and result have identical keys | Check whether the function reindexes keys or retains original associations. |
| Confusing mutation with a returned copy | Negative offsets count from the end. String keys are retained; numeric keys normally reset unless preserve_keys=true. |
| Choosing a related function with different behavior | array_splice() removes or replaces the selected part and modifies the input. |
Key Takeaways
array_slice()helps you extract a portion of an array without changing the original.- Negative offsets count from the end. String keys are retained; numeric keys normally reset unless preserve_keys=true.
- array_splice() removes or replaces the selected part and modifies the input.
- Use the practical example as a starting point, but validate real application inputs.
array_slice() is useful when you need to extract a portion of an array without changing the original. Use its documented rules and the examples above to choose the right behavior for your data.