Menu

PHP array_slice(): Get Part of an Array

array_slice($array, $offset, $length) returns a copy of part of an array without changing the original. Learn negative offsets for the last items, preserve_keys, paging through a list, and how it differs from array_splice.

This page includes runnable editors - edit, run, and see output instantly.

array_slice($array, $offset, $length) returns a new array with $length elements starting at position $offset. The original array is not changed. Leave out $length to take everything up to the end.

The offset is a position (0 is the first element), not a key. That matters for associative arrays and for arrays whose keys are not 0, 1, 2.

Get the last N elements with a negative offset

A negative offset counts from the end: -1 is the last element, -3 starts three from the end. A negative length stops that many elements before the end.

Asking for more elements than exist is fine: array_slice($log, 0, 100) returns all five.

Keep the original keys with preserve_keys

Integer keys are renumbered from 0 unless you pass true as the fourth argument. String keys are always kept.

Use true when the keys mean something, such as database ids, and you will look elements up by them later.

Paginate an array with array_slice

Showing page $page of a list with $perPage items per page is one slice: the offset is ($page - 1) * $perPage. This example prints an HTML list with page links. Run it and click a page number: the same script runs again with $_GET['page'] set from the link.

The max(1, min($pages, ...)) line keeps a hand-typed ?page=99 or ?page=-1 inside the valid range. With a database you would ask for only one page with LIMIT and OFFSET instead of loading every row, but for a list already in memory, array_slice is the whole job.

array_slice vs array_splice

The names differ by one letter and they do opposite things to the original. array_slice copies a part out. array_splice cuts the part out of the array you pass, renumbers it, and returns the removed elements. It can also insert replacements where the cut was.

array_slicearray_splice
Changes the originalnoyes (by reference)
Returnsthe selected partthe removed part
Can insert elementsnoyes, fourth argument
Integer keysrenumbered unless preserve_keysrenumbered

For more ways to delete elements, see removing elements from an array.

Split an array into chunks

When you need every page at once, for example to lay products out in rows of three, array_chunk does the repeated slicing for you.

Change the 3 to 2 or 4 and run it again. The last chunk holds whatever is left over.

Frequently Asked Questions

How do I get the first N elements of an array in PHP?

array_slice($arr, 0, 3) returns the first three elements. If the array has fewer than three, you get all of them and no error.

How do I get the last N elements of an array?

Use a negative offset: array_slice($arr, -3) returns the last three elements. Leaving out the length means "to the end".

What does preserve_keys do in array_slice?

By default array_slice renumbers integer keys from 0. Pass true as the fourth argument, array_slice($arr, 2, 3, true), to keep the original integer keys. String keys are always kept, with or without it.

What is the difference between array_slice and array_splice?

array_slice returns a copy of part of the array and leaves the original alone. array_splice takes the array by reference, removes that part from it (optionally inserting replacements) and returns what it removed.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED