Backend Development 4 min read

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

This article explains how PHP's array_slice() function works, detailing its parameters, basic syntax, and providing three clear code examples that demonstrate extracting subsets of arrays, preserving keys, and using the function for pagination.

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

In PHP development, array manipulation is frequent, and PHP offers many powerful array functions; one especially useful function is array_slice() . This article introduces how to use array_slice() and provides code examples to help readers understand its behavior.

The array_slice() function extracts a portion of an array and returns a new array. It accepts three main parameters: the original array, the starting offset, and the length of elements to extract. If the length is omitted, the function slices from the offset to the end of the array.

The basic syntax is:

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

Where $array is the source array, $offset is the index at which to start, $length is the number of elements to take, and $preserve_keys determines whether original keys are retained.

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 part of the array

<?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 keep the original keys.

In real-world development, 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. To change the original array, assign the result back to it.

In summary, array_slice() is a highly practical PHP array function that provides flexible data handling by allowing developers to extract sub‑arrays efficiently.

backend developmentpaginationPHParray-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

login 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.