PHP array_slice() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_slice() returns a part of an array without changing the original.

You choose where to start with an offset and how many items to take with a length. It is perfect for pagination and for taking the first or last few items.

Syntax

Call array_slice() with the values you need. Start with this line.

PHP
$part = array_slice($letters, 1, 3);
CallWhat it does
array_slice($letters, 1, 3)Returns 3 items starting at position 1.
array_slice($letters, -2)Returns the last 2 items.
array_slice($items, 1, null, true)Keeps the original keys in the result.

array_slice() never changes the original array. If you want to remove items from the original too, use array_splice().

Library declarations
PHP
array_slice(array $array, int $offset, ?int $length = null, bool $preserve_keys = false): array

Parameters

ParameterDescription
arrayRequired. The input array.
offsetRequired. Where to start. A negative number counts from the end of the array.
lengthOptional. How many items to return. null (the default) means take everything to the end. A negative length stops that many items before the end.
preserve_keysOptional. If true, numeric keys are kept. By default they are renumbered from 0. String keys are always kept.

Return Value

Return ValueDescription
arrayA new array holding the selected slice. It is empty if the offset is beyond the end.

PHP Examples

Example 1 — Take Part of a List

Take three items starting from position 1.

PHP
<?php
$letters = ["a", "b", "c", "d", "e"];
$part = array_slice($letters, 1, 3);

echo implode(", ", $part), PHP_EOL;

How It Works

  1. Create a list of five letters. Positions start at 0.
  2. Offset 1 starts at "b" and length 3 takes three items.
  3. The slice is a new array with "b", "c" and "d".
  4. implode() prints the items separated by commas.

Example 2 — Negative Offsets and Keys

Count from the end, and decide whether to keep numeric keys.

PHP
<?php
$letters = ["a", "b", "c", "d", "e"];
echo implode(", ", array_slice($letters, -2)), PHP_EOL;

$ids = [10 => "x", 11 => "y", 12 => "z"];
echo json_encode(array_slice($ids, 1)), PHP_EOL;
echo json_encode(array_slice($ids, 1, null, true)), PHP_EOL;

How It Works

  1. An offset of -2 starts two items from the end, so the slice is "d" and "e".
  2. The $ids array uses keys 10, 11 and 12.
  3. By default the slice is renumbered, giving a plain list ["y","z"].
  4. With preserve_keys set to true, the original keys 11 and 12 stay.

Example 3 — Simple Pagination

Show page 2 of a list when each page holds 3 items.

PHP
<?php
$items = range(1, 10);
$page = 2;
$perPage = 3;

$offset = ($page - 1) * $perPage;
$pageItems = array_slice($items, $offset, $perPage);

echo "Page ", $page, ": ", implode(", ", $pageItems), PHP_EOL;

How It Works

  1. range(1, 10) builds the numbers 1 to 10.
  2. The offset for page 2 is (2 - 1) * 3 = 3, which is where the number 4 sits.
  3. array_slice() takes 3 items from that offset.
  4. The result is 4, 5 and 6, printed with a page label.

Frequently Asked Questions

It returns a selected part of an array as a new array. The original array is left unchanged.
array_slice() only copies a part of the array. array_splice() removes (and can replace) items in the original array.
Use a negative offset: array_slice($array, -3).
By default numeric keys are renumbered from 0. Pass true as the fourth argument to preserve them.
Yes. String keys are always preserved. The preserve_keys flag only affects numeric keys.

Next: array_splice()

array_splice() removes items from an array and can insert new items in their place.

array_splice() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful