Using PHP's array_slice() Function: Syntax, Parameters, and Practical Examples

This article introduces PHP's array_slice() function, explains its parameters and syntax, and provides three practical code examples demonstrating how to extract subsets of an array, preserve keys, and use it for pagination.

php Courses
php Courses
php Courses
Using PHP's array_slice() Function: Syntax, Parameters, and Practical Examples

In PHP development, manipulating arrays is a common task. The built-in array_slice() function allows you to extract a portion of an array and return it as a new array.

The function accepts up to four arguments: the source array, the offset (starting index), the length (number of elements to extract), and an optional $preserve_keys flag that determines whether the original keys are retained.

Basic syntax:

array array_slice(array $array, int $offset, int $length = null, bool $preserve_keys = false)

Parameters: $array: The original array to slice. $offset: Index at which to start extraction (negative values start from the end). $length: Number of elements to extract; if omitted, extraction continues to the end of the array. $preserve_keys: When true, original keys are kept; otherwise they are reindexed.

Example 1 – Extract the first three elements:

<?php
$array = [1, 2, 3, 4, 5, 6];
$subset = array_slice($array, 0, 3);
print_r($subset);
?>

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

Example 2 – Extract the last two elements while preserving keys:

<?php
$array = [1 => 'a', 2 => 'b', 3 => 'c', 4 => 'd', 5 => 'e', 6 => 'f'];
$subset = array_slice($array, -2, 2, true);
print_r($subset);
?>

Output:

Array
(
    [5] => e
    [6] => f
)

Example 3 – Omit the length parameter to get the remaining elements from a given offset:

<?php
$array = ['apple', 'banana', 'orange', 'grape', 'watermelon'];
$subset = array_slice($array, 2);
print_r($subset);
?>

Output:

Array
(
    [0] => orange
    [1] => grape
    [2] => watermelon
)

These examples show that array_slice() is simple yet powerful. It can extract any part of an array and optionally retain the original keys.

In real‑world applications, array_slice() is often used for pagination by adjusting the $offset and $length parameters to display different pages of data.

Note that array_slice() does not modify the original array; it returns a new array. If you need to change the original array, assign the result back to it.

In summary, array_slice() is a versatile PHP array function that provides flexible data handling for backend development.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backendphp-functionsarray manipulationarray_slice
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.